Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 19th, 2004, 04:23 PM
ryankask ryankask is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Posts: 52 ryankask User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 44 sec
Reputation Power: 7
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

Reply With Quote
  #2  
Old June 19th, 2004, 05:04 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Default values are evaluated only once, and they are evaluated at definition time.

Last edited by percivall : June 19th, 2004 at 05:10 PM.

Reply With Quote
  #3  
Old June 19th, 2004, 07:00 PM
ryankask ryankask is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Posts: 52 ryankask User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 44 sec
Reputation Power: 7
If I just pass the instance in the function header, will it include the instance's attributes?

Reply With Quote
  #4  
Old June 20th, 2004, 02:12 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
  #5  
Old June 20th, 2004, 09:09 AM
ryankask ryankask is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Posts: 52 ryankask User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 44 sec
Reputation Power: 7
OK thanks, the problem was solved.

Reply With Quote
  #6  
Old June 20th, 2004, 09:18 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Just out of interst, can i ask what solusion you came up with for this problem?
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #7  
Old June 20th, 2004, 08:35 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to 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
__________________
Debian - because life's too short for worrying.
Best. (Python.) IRC bot. ever.

Reply With Quote
  #8  
Old June 21st, 2004, 05:49 PM
ryankask ryankask is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Posts: 52 ryankask User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 44 sec
Reputation Power: 7
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.

Reply With Quote
  #9  
Old June 22nd, 2004, 12:35 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Mathod in class has undefined name 'self'??


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT