It depends. Usually, you would declare the variable in __init__(). It has
two advantages:
1. Your code becomes more readable and easy to understand. Looking at the
__init__() one can tell right away what variables will be used.
2. All the methods using that variable anywhere in the class will have no
trouble finding it.

It is a well-known python idiom where if a variable is not found, you can
create it on the fly and then it becomes available for to all the instance
methods.
try:
    myVar
except AttributeError:
    # create my var
    self.my_var = 'some var'

Also, if you need to create the varaible using some method then it is easy
to do in __init__ also:
class Foo(object):
    def __init__(self, *args, **kwargs):
        self.my_var = self._get_my_var()

   def _get_my_var(self):
        return 'some var'



On Wed, May 17, 2017 at 7:41 PM, Justin Israel <[email protected]>
wrote:

>
>
> On Wed, May 17, 2017, 9:39 PM Rudi Hammad <[email protected]> wrote:
>
>> Hi, another question about programing style.
>> Many times I create a variable in a method (not the __init__)  that I use
>> in another method. And to do this I have to make that variable an attribute
>> of object with self.myVariable
>> My question is if I it is okey to do this:
>>
>> def foo(self):
>>     myVar = "something"
>>     cmds.joint(n=myVar)
>>     ....more code here that uses myVar
>>     ....
>>     ....
>>     self.myVar = myVar
>>
>>
>> Is this a normal thing to do, or should I right directly from the
>> beginning self.myVar =  "something"
>> The main reason is to keep things a little bit shorter, and not see self.
>> repited all over the code.
>>
>
> As long as you don't trigger any methods in your elided  "more code" which
> will rely on that attribute to exist, then it would be fine.
>
> It is normally safer to set the fields to a default value in your__init__.
> Especially public attributes.
>
>
>>
>> --
>> 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/4c554812-164b-43dc-a62b-
>> ce3e3bf0b209%40googlegroups.com
>> <https://groups.google.com/d/msgid/python_inside_maya/4c554812-164b-43dc-a62b-ce3e3bf0b209%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> 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/CAPGFgA0%2BAqQ-Dh3h-CpnPkGAtoFJKAaRWKKmEs-%
> 3DJUNXciSyYQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0%2BAqQ-Dh3h-CpnPkGAtoFJKAaRWKKmEs-%3DJUNXciSyYQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> 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/CAPaTLMTL-R6m69F3D33TKDD6BcuhhR8%2Bfngf4x71_ngyoogPGA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to