variable scope of class objects
Terry Reedy
tjreedy at udel.edu
Mon Oct 19 20:03:43 EDT 2015
On 10/19/2015 7:19 PM, sohcahtoa82 at gmail.com wrote:
> Class variables are accessible without creating an instance of a class. Also, changing the value of a class variable affects ALL instances of that class. This is because the variable belongs to the class itself, not any of the instances of that class.
>
> "self" is used to tell the interpreter that the variable/function you are accessing is a member of an instance of that class.
Wrong. The first parameter of a method is an instance of a class. It
is conventionally called 'self' but could be any other name. I use 's'
for quick private test examples.
> Here's an example:
>
> class MyObject(object):
> count = 0
> def __init__(value):
This should be def __init__(self, value):
> MyObject.count += 1
> self.value = value
>
> def printStuff():
def print(self):
> print("My value is ", self.value)
> a = MyObject('a')
In 2.7.10
TypeError: __init__() takes exactly 1 argument (2 given)
Please run code before posting.
--
Terry Jan Reedy
More information about the Python-list
mailing list