943,754 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 600
  • Python RSS
Sep 5th, 2009
0

understanding the self in relation to variables

Expand Post »
I understand that python provides a value for self when methods are called on the instance of a class.
However, I don't understand how self works when assigning value to a variable of instance.
Python Syntax (Toggle Plain Text)
  1. class snake:
  2. def __init__(self,name):
  3. self.name=name#this is the bit I don't understand. How is self.name #interpreted by python?
Last edited by mahela007; Sep 5th, 2009 at 9:18 am.
Reputation Points: 16
Solved Threads: 2
Posting Whiz in Training
mahela007 is offline Offline
214 posts
since Feb 2009
Sep 5th, 2009
1

Re: understanding the self in relation to variables

Look over this code sample, it should be very explanatory:
python Syntax (Toggle Plain Text)
  1. # role of self in Python classes
  2. # self can be named different, but 'self' is convention
  3.  
  4. class Snake:
  5. def __init__(self, name):
  6. # self keeps track of each instance
  7. # and also makes self.name global to class methods
  8. self.name = name
  9. # test
  10. print(self)
  11.  
  12. def isnice(self):
  13. # a class method has self as the first argument
  14. return self.name + " is very nice"
  15.  
  16. # create 2 instances of class Snake
  17. bob = Snake('Bob Python')
  18. mary = Snake('Mary Rattle')
  19.  
  20. print('-'*40)
  21.  
  22. # now you can get the name that has been assigned to self.name
  23. print(bob.name)
  24. print(mary.name)
  25.  
  26. # access the class method
  27. print(mary.isnice())
  28.  
  29. """my result -->
  30. # self for each instance has a different location in memory
  31. <__main__.Snake object at 0x01E0B2B0>
  32. <__main__.Snake object at 0x01E0B090>
  33. ----------------------------------------
  34. Bob Python
  35. Mary Rattle
  36. Mary Rattle is very nice
  37. """
Last edited by Ene Uran; Sep 5th, 2009 at 11:25 am.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Sep 5th, 2009
0

Re: understanding the self in relation to variables

thanks!
Reputation Points: 16
Solved Threads: 2
Posting Whiz in Training
mahela007 is offline Offline
214 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Building an ISO for Python project
Next Thread in Python Forum Timeline: How to pass an instance of a class as and argument ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC