|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Default values are evaluated only once, and they are evaluated at definition time.
Last edited by percivall : June 19th, 2004 at 05:10 PM. |
|
#3
|
|||
|
|||
|
If I just pass the instance in the function header, will it include the instance's attributes?
|
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
OK thanks, the problem was solved.
|
|
#6
|
||||
|
||||
|
Just out of interst, can i ask what solusion you came up with for this problem?
|
|
#7
|
|||
|
|||
|
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
|
|
#8
|
|||
|
|||
|
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.
|
|
#9
|
||||
|
||||
|
Quote:
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Mathod in class has undefined name 'self'?? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|