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

    Join Date
    Mar 2005
    Posts
    133
    Rep Power
    11

    A few Class questions


    well how about my code first.
    its really just some simple code to get used to the envirement(sp?)
    Code:
    class classtest:
        sttr="test a string";
        def fname(self):
            return "Testing function"
    
    getclass=classtest();
    print getclass.sttr;
    print getclass.fname();
    but all this does is give me a window for 0.5 seconds or something. eh :P


    and for the question:
    ive read 2 tutorials on classes (including one on DevShed) but really dont get what the self argument is for.
    can someone explain what that does and how to use it?! =/
    same for __init__, they keep on talking about that stuff but they really dont explain it to you in english
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Code:
    class classtest
        def __init__(self):
            sttr = 'test a string';
        def fname(self):
            return 'Testing function';
    getclass = classtest();
    print getclass.sttr;
    print getclass.fname();
    The self argument alows a variable to be used "globally" throughout the entire class.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    133
    Rep Power
    11
    Originally Posted by †Yegg†
    Code:
    class classtest
        def __init__(self):
            sttr = 'test a string';
        def fname(self):
            return 'Testing function';
    getclass = classtest();
    print getclass.sttr;
    print getclass.fname();
    The self argument alows a variable to be used "globally" throughout the entire class.
    Thanks, i understand them a bit better now

    the code doesnt work though, you were missing a colon, but it still wont work :P
    know what's up?
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    I left something out, I modifed the code for you, here it is from my Python Interpreter.
    Code:
    >>> class classtest:
    	def __init__(self):
    	    self.sttr = 'test a sring';
    	def fname(self):
    	    return 'Testing function';
    
    	
    >>> classtest()
    <__main__.classtest instance at 0x01C898F0>
    >>> getclass = classtest()
    >>> getclass.sttr
    'test a sring'
    >>> getclass.fname()
    'Testing function'
    >>>
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    133
    Rep Power
    11
    Thanks yegg

    also can someone explain these word for me :
    Derived class
    base class
    data attribute


  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    The following link should explain everything about the derived and base classes:
    http://www.answers.com/topic/class-c...rived&hl=class
    You can learn about what a data attribute is at the following link:
    http://www.answers.com/topic/object-...a&hl=attribute
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0
    why do you guys keep ending your statements with semi-colons. its allowed in python but not needed. You only use it when you are making compound statements. ex
    Code:
    print "hello"; print "yellow"
    not the best example but my brain is mush right now. the point is don't use it, its unnecessary.

    as to what the self parameter is in a bound method. a method in a class is simply a function that is bound to that class. So the self method simply refers to the class itself. you could use the name of the class if you wanted but by convention self is used instead. So when the function is called, its first parameter by default it takes is the name of the class. That is really the only diff between functions and methods (functions belonging to a class)
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    133
    Rep Power
    11
    i get the self parameter now.

    and i use semi-colons because i regularly code in php and just started coding in C.
    so it's kinda hard to code with semi-colons for those language and without for Python and Javascript.
    so i just add semi-colons to the ones who dont need it, but still accept it
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0
    its your perogative of course, but python is supposed to be a rapid application programming language. the time you take to put in those semi colons adds up, makes your code less readable and goes against the essence of what makes python python. I too code in C predominently and i have no problems ommiting that character.

    Comments on this post

    • Yegg` disagrees : I completely disagree.
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  18. #10
  19. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    I completely disagree with what you said. Adding in the semi colons taking what, ONE extra second for each time you add it in? If 10000 lines of code contained the semicolon at the end, that would only be an extra 166 minutes. And that is if you added them all after you were done coding. When you're adding the semicolon in as you're coding, the time in which you use to type in the semicolon doesn't even add up. ! single second if that is not a big difference. I personally use it just because I like that syntax.

IMN logo majestic logo threadwatch logo seochat tools logo