On Mon, May 16, 2016 at 10:31 AM <[email protected]> wrote:
> hello,
>
> im trying to get back variables that have been generated in a function.
>
> def func(condition):
>
> if conditon1 == True:
> var1="one"
> var2="two"
> var3="three"
>
> else: condition2 ==True:
> var1="secondOne"
> var2="secondTwo"
>
> return var1, var2, var3
>
> however how do you only return the number of variables that have been
> generated on one particular call to the function?. so in this case if the
> function is called with a particular condition it will create 3 variables
> in which case i want the function to return the three variable values. But
> if the function is called with condition 2 it will create only two
> variables in which case i want it to return only two variables.
>
> does anyone know how the return part of the function can automatically
> change to return only the amount of variables that have been set on the
> current function call?
>
> may not have explained this properly
>
It is generally bad practice to return different types of values and
different number of values from a function. Exceptions of course, if it is
clearly documented that certain flags to the function product different
behavior. But you should strive to have your functions be consistent in
their return values to make your code cleaner and avoid bugs.
To address your code example, an immediate solution to make your return
consistent would be do set default values for everything:
var1 = None
var2 = None
var3 = None
# condition# condition
return var1, var2, var3
Now you always return the same number of values, but some of them may be
None. And if you end up having too many return values then you should
consider creating some kind of Result type:
class Result(object):
def __init__(self):
self.var1 = None
self.var2 = None
self.var3 = None
But if these types can be completely different, maybe what you are after is
two functions, instead of trying to put the logic for multiple functions
into a single one and trying to accommodate a uniform return logic? You
would have to be more specific about what "condition" is here for any more
details on that.
>
> thanks,
> Sam
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/d0079dc2-20e4-499f-a881-fd89449861ff%40googlegroups.com
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA04kx9naUF9xwy_zbfoc3wU8uExyBdBy4qXLgMgXYHL6g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.