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

    Join Date
    Dec 2001
    Posts
    52
    Rep Power
    13

    Question Mathod in class has undefined name 'self'??


    I get the following error:

    Traceback (most recent call last):
    File "<pyshell#188>", line 1, in -toplevel-
    class writer:
    File "<pyshell#188>", line 27, in writer
    def createFile(self, file = self.file, path = self.path, ext = self.ext):
    NameError: name 'self' is not defined

    because of this line:

    def createFile(self, file = self.file, path = self.path, ext = self.ext):

    Why would this be?

    Thanks,
    Ryan
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    Default values are evaluated only once, and they are evaluated at definition time.
    Last edited by percivall; June 19th, 2004 at 05:10 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    52
    Rep Power
    13
    If I just pass the instance in the function header, will it include the instance's attributes?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    If you pass an instance you pass a reference to the instance. I don't really understand what you mean by including the instance's attributes; you're passing a reference to the instance. Of course you'll be able to access the instance's attributes.

    I'm not sure if you're still confused about the NameError or not. You have to understand that self is not a magic name, there's really nothing special about it. But when you call the method the instance you prepend to the method is given "automatically" as the first argument. Only then is "self" defined. If you'd try to lookup the name self before then (as for instance when you define the method) you'd not find a bound name; it's undefined before run-time.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    52
    Rep Power
    13
    OK thanks, the problem was solved.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Just out of interst, can i ask what solusion you came up with for this problem?
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    The appropriate solution would be:
    Code:
    def createFile(self, file=None, path=None, ext=None):
        if file is None:
            file = self.file
        if path is None:
            path = self.path
        if ext is None:
            ext = self.ext
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    52
    Rep Power
    13
    The solution wasn't really a solution at all, but rather just fixing a silly mistake. the __init__() function initialized three instance attributes, and I was attempting to pass them through the function directly instead of just passing the instance itself which has the attributes already. Just a stupid error from a newbie.
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by Strike
    The appropriate solution would be:
    Code:
    def createFile(self, file=None, path=None, ext=None):
        if file is None:
            file = self.file
        if path is None:
            path = self.path
        if ext is None:
            ext = self.ext
    Ok, that is how i'd do it, and how i've seen a million other users and tutorials do it. Was just wondering if you found another way thats all .

    Later guys,

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


IMN logo majestic logo threadwatch logo seochat tools logo