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 October 28th, 2004, 12:02 AM
NewPythoner NewPythoner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Bombay, India
Posts: 159 NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 43 m 45 sec
Reputation Power: 7
Send a message via Yahoo to NewPythoner
Unclear abt 'self'!

Hi,
I wanted to get a clearer definiton of 'self' which is used in classes...cld anyone do it??
Thanks & Rgds!
Subha

Reply With Quote
  #2  
Old October 28th, 2004, 12:49 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,676 Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 21 m 12 sec
Reputation Power: 1083
self is traditionally used by a class to refer to an instance of itself. It is similar to the this pointer in C++/java and the self keyword of Delphi. The one difference is that Python doesn't reserve the word 'self' as a keyword and you could use any other variable name in place of 'self' and it would work fine, though the python convention is to use 'self'. In the below code, note that func2() uses 'myvar' as the first argument, but the code does the same thing as func().
Code:
class myclass:
   def func(self, val):
       self.somevalue = val

   def func2(myvar, val):
       myvar.somevalue = val

x = myclass()
y = myclass()

x.func()
y.func()


As to what self does, when you call x.func(), the value of self within func() will be set as a reference to x. When you call y.func(), the value of self within func() will be set as a reference to y. Thus, you can see that 'self' is set to point to the instance of the variable that called func().
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month

Last edited by Scorpions4ever : October 28th, 2004 at 10:30 AM.

Reply With Quote
  #3  
Old October 28th, 2004, 01:55 AM
NewPythoner NewPythoner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Bombay, India
Posts: 159 NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 43 m 45 sec
Reputation Power: 7
Send a message via Yahoo to NewPythoner
Thanks Scorpy for that crash course on 'self'. It is indeed like the 'this' pointer ...but as far as my knowledge goes...the 'this' pointer is an hidden parameter...not explicitly specified. I understood that 'self' has to be the first argument... can have any name... n its mandatory. Now I've got one more question....is _init_ like constructor???

Thanks & Rgds,
Subha

Reply With Quote
  #4  
Old October 28th, 2004, 02:58 AM
Ether_Sa Ether_Sa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Location: FL
Posts: 19 Ether_Sa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to Ether_Sa
it's the closest thing to a constructor, since it's the first piece of code run in an instance of a class, but its not because the object has already been constructed by the time __init__ is called, and you already have a valid reference to the new instance of the class.

Reply With Quote
  #5  
Old October 28th, 2004, 07:47 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 10 m 32 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
In Python, the construction of the instance is preformed by __new__() and then as Ether said __init__() is called, along with any arguments used in initialisation. You could easily think of it as being a constructor in the basic C/C++ sense of the word, since it is used for the same kind of things, but there are some differences. Nothing worth worrying about though .

You won't usually overwrite __new__(), though you can if you need to! For an example of this being done check out Grims ptypes module:

http://www.peck.org.uk/p/python/
http://www.peck.org.uk/pycodev/modules/articles/article.php?id=6

Nice explanation of self scorp!

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


Reply With Quote
  #6  
Old October 29th, 2004, 12:18 AM
NewPythoner NewPythoner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Bombay, India
Posts: 159 NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 43 m 45 sec
Reputation Power: 7
Send a message via Yahoo to NewPythoner
Thanks Ether!
And hey thanks Mr.moderator for the links & also for introducing me to __new__
Its rightly said instead of wading thru' zillions of books if one talks to a person having a good knowledge abt the subject...one can learn a lot!!!

Thanks everyone,
Subha

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Unclear abt '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 5 hosted by Hostway
Stay green...Green IT