declaring variables on top of the function?

63 views
Skip to first unread message

Rudi Hammad

unread,
May 12, 2017, 7:36:01 AM5/12/17
to Python Programming for Autodesk Maya
Hello,
quick question. Do you declare the variables on top of the function like this:

def myFunc():
   
"""The description"""
    listA
= []
    listB
= []
   
   
'''
    some code in the middle
    bla bla
    '''

   
for i in range(10):
        listA
.append(i)
       
   
'''
    more code in the middle
    bla bla
    '''

   
for j in range(5):
        listB
.append(j)
    
or would you do it like this?

def myFunc():
   
   
'''
    some code in the middle
    bla bla
    '''

    listA
= []
   
for i in range(10):
        listA
.append(i)
       
   
'''
    more code in the middle
    bla bla
    '''

    listB
= []
   
for j in range(5):
        listB
.append(j)



In the second case, for me, it does make sense declaring the variable right before the loop, because then you read it like a paragraph.
in the fist case, declaring everything at the beginning looks cleaner, but makes you having to track where the variable comes from.
Is this a personal preference?If so, I am not sure which makes more sence.

thanks

Alok Gandhi

unread,
May 12, 2017, 7:56:39 AM5/12/17
to python_in...@googlegroups.com
I prefer and use the second one.

--
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 python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/477cb809-6a81-4e21-8168-b8f9c95fda81%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Justin Israel

unread,
May 12, 2017, 5:36:30 PM5/12/17
to python_in...@googlegroups.com

I also prefer the second one. Declaring all of your variables at the top of the function body is a very C language thing to do.


To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.



--

--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPaTLMR1f6JrVrAwr4nOm8x9W5tX%2B3B4SK6R8930wJ%3DxggwS%3DA%40mail.gmail.com.

Rudi Hammad

unread,
May 13, 2017, 3:16:34 PM5/13/17
to Python Programming for Autodesk Maya
Noted. Thanks.
I´ll stick with the second one.

Rudi Hammad

unread,
May 17, 2017, 5:39:39 AM5/17/17
to Python Programming for Autodesk Maya
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.


Justin Israel

unread,
May 17, 2017, 7:41:51 AM5/17/17
to Python Programming for Autodesk Maya
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 python_inside_m...@googlegroups.com.

Alok Gandhi

unread,
May 17, 2017, 11:07:58 AM5/17/17
to python_in...@googlegroups.com
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 <justin...@gmail.com> wrote:


On Wed, May 17, 2017, 9:39 PM Rudi Hammad <rudih...@gmail.com> 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 python_inside_maya+unsub...@googlegroups.com.

--
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 python_inside_maya+unsub...@googlegroups.com.
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.

For more options, visit https://groups.google.com/d/optout.



--
Reply all
Reply to author
Forward
0 new messages