Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
Newbie question on self in classes
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Peter Otten, post: 1745489"] The name "self" is just a convention. Python passes the instance to a method, i. e. a def ... inside the class as its first parameter: .... def method(self): .... self.value = 2 .... So you could say that self is accessible inside (normal) methods. Now let's create an instance: Calling the method: This is just a shortcut for: with the important difference that the first form will automatically pick the "right" method when inheritance comes into play. Now to the value attribute. The way you defined it, it is a class attribute, something that ist best avoided at this early state of the learning curve. What you want is rather an instance attribute, i. e. something that may be different for every instance of the class. These are initialized in the __init__() method, which is called implicitly when you say Test(): .... def __init__(self): .... self.value = 1000 .... def method(self, newvalue): .... self.value = newvalue .... 3210 Peter [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
Newbie question on self in classes
Top