#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    20
    Rep Power
    0

    Declaring class members at start of class


    Hi, I am used to declaring my class members at the begining of a class. I have read the Python tutorial and seen you can just declare it anywhere you want.

    Personally I think it is clearer for large class to have all the members at the start of the class.

    When I do this I get a : is not defined error.

    Should I change my thinking or is there a way to do this.
    While searching for a solution I came across the global keyword which may be what im looking for but I find it weird to use

    global __myVar

    To declare a private variable ( __ ) with the global keyword...
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Python doesn't include a way to declare a variable, but it is fairly common to see variables defined with default values i.e. None, 0, '', [], {}, (). With the exception of None, each of these gives a nice hint about the type of data your variable should hold .

    This works well for me, what do you think?

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    Bimingham,AL
    Posts
    68
    Rep Power
    11
    I do the same as netyan , i declare global varibles with the default type they might hold in the future , like
    self.a = []
    self.b= False


    hope that helps
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    20
    Rep Power
    0
    If I do this :

    global __currentState, __states, __matrix

    __currentState = 0
    __states = []
    __matrix = []

    or this :

    global __currentState, __states, __matrix

    self.__currentState = 0
    self.__states = []
    self.__matrix = []

    I get an error on the first assignment operation.

    I also get an error if I only do this :

    __currentState = 0
    __states = []
    __matrix = []

    or this :

    self.__currentState = 0
    self.__states = []
    self.__matrix = []

    Giving me a syntax error and undefined self error respectively.

    What is the correct syntax to do this. The Python tutorial does not cover this in details.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    One way would be:

    Code:
    class Dog:
        
        def __init__(self):
            self.barkVolume = 10
            self.knownDogs = []
            self.knownSmells = {}
    
        def bark(self):
            print "woof"
    I'm not clear on the differences if you do:

    Code:
    class Dog:
        barkVolume = 10
    Global is rarely needed or used, and __name doesn't do anything - it's just to show other people that they shouldn't change that variable. It doesn't actually stop them. So there isn't much point in using it with members of a class.
    Last edited by sfb; February 19th, 2005 at 08:44 AM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    20
    Rep Power
    0
    I've looked into Python code libraries for reference and notices no one is declaring, maybe I should just stop being an old C/C++ coot and get with the Python way !
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    In general Python users are a pretty open bunch and no one will penalize you for doing this in your own way. Personally, if I was defining a bunch of member variables (also called attributed in Python) I would do it like this:

    Code:
    class Foo:
        anInt = 0
        aString = ''
        aList = []
    On the other hand if I were setting up some instance variables then I'd do it in the __init__() method, and use the methods arguments to to set the instance variables without declaring them:

    Code:
    class Bar:
        def __init__(self, anInt, aString, aList = []):
            self.anInt = anInt
            self.aString = aString
            self.aList = aList
    Have fun ,

    Mark.
    Last edited by netytan; February 19th, 2005 at 11:30 AM.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo