Anda di halaman 1dari 4

A class has the following syntax:

1 c l a s s ClassName :
2 <s t a t e m e n t −1>
3 .
4 .
5 .
6 <s t a t e m e n t −N>

An instance is an object which has been instantiated from a class. A

method is a function that “belongs to” an object. In Python, the term

method is not unique to class instances: other object types can have meth-

ods as well. For example, list objects have methods called append, insert,

remove, sort, and so on.

Class variables are variables that belong to the class as a whole; there

is only one copy of each one. Instance variables or attributes is data that

belongs to individual objects; every object has its own copy of each one.

Member variables refer to both the class and instance variables that are

defined by a particular class

Class methods are methods or functions that belong to the class as a

whole and have access only to class variables and inputs from the procedure

call. Instance methods belong to individual objects, and have access to

instance variables for the specific object they are called on, inputs, and class

variables.

Now what can we do with instance objects? The only operations under-

stood by instance objects are attribute references. There are two kinds of

valid attribute names, instance variables and methods.


1 c l a s s Dog :
2 def init ( s e l f , name ) :
3 s e l f . name = name # i n s t a n c e v a r i a b l e u n i q u e t o each i n s t a n c e
4 d e f spam ( s e l f ) :
5 p r i n t ( ’ H e l l o World ! My name i s ’+s t r ( s e l f . name )+ ’ ! ’ )

1
The class here is Dog, which has method, spam. The command, Dog.spam,

is a function object. The variable it is assigned to, say:


1 f c n o b j = Dog . spam

returns the method (which is just a function) spam.

Note that the function object, fcn_obj, is not an instance, nor does it

have any links to an instance. It simply implements a method in a class. So

it does not have access to other variables or methods.

Prior to Python 3.X, an ‘unbound method’ was implemented. An un-

bound method was really just a thin wrapper around some function object,

but it enforced a restriction that the first argument had to be an instance

of the class in which the method was defined. Thus, if someone wanted to

call fcn_obj as a function, they would have to pass an instance of class Dog

as the first argument. For example:


1 >>> doggo = Dog ( ) # c r e a t i n g an i n s t a n c e c a l l e d doggo
2 >>> f c n o b j
3 <unbound method Dog . spam>
4 >>> f c n o b j ( doggo ) # c a l l i n g Dog . spam w/ i n p u t , doggo
5 ’ H e l l o World ’

A related problem occurs if someone writes a Python statement that refers

to a method on a specific instance of an object. For example:


1 >>> method obj = doggo . spam

The variable, method_obj, is called a method object. Like the function

object, fcn_obj, it refers to the method, spam from class Dog. But this

time, the reference was obtained through the instance, doggo. To handle

this situation, a different callable object known as a ‘bound method’ is used.

This object is also a thin wrapper around the function object for the method.

However, this wrapper implicitly stores the original instance that was used

to obtain the method. Thus, a later statement such as method_obj() will

2
call the method, Dog.spam, with the instance, doggo, implicitly set as the

first argument:
1 >>> method obj = doggo . spam
2 >>> method obj ( )
3 ’ H e l l o World ! My name i s Woof ! ’
4 >>> method obj
5 <bound method Dog . spam o f < main . Dog o b j e c t a t 0 x0000000006083978>>
6 >>>
7 >>> Dog . spam ( doggo )
8 ’ H e l l o World ! My name i s Woof ! ’

So method_obj() is equivalent to Dog.spam(doggo). We also see that the

method object, method_obj, is a bound method to the Dog.spam method

and is ‘bound’ to the instance doggo.

The restriction on ‘unbound methods’ has been removed in Python 3.X

but ‘bound methods’ still remain. So the command, Dog.spam, now returns

a plain function object:


1 >>> f c n o b j = Dog . spam
2 <f u n c t i o n main . Dog . spam>

Note the differences between a method object like method_obj() and

a function object like fcn_obj(). A method object is a bound object and

references a method in a class and an instance of that class. It is ‘bound’ to

this instance. It therefore implicitly sets the first argument of the method it

is referencing to be the instance. A function object is now simply an object

referencing a method in a class and is not linked to any instance.

3
Generally speaking, instance variables are for data unique to each in-

stance and class variables are for attributes and methods shared by all in-

stances of the class:


1 c l a s s Dog :
2 kind = ’ canine ’ # c l a s s v a r i a b l e s h a r e d by a l l i n s t a n c e s
3 def init ( s e l f , name ) :
4 s e l f . name = name # i n s t a n c e v a r i a b l e u n i q u e t o each i n s t a n c e
5 >>> d = Dog ( ’ Fido ’ )
6 >>> e = Dog ( ’ Buddy ’ )
7 >>> d . k i n d # s h a r e d by a l l dogs
8 ’ canine ’
9 >>> e . k i n d # s h a r e d by a l l dogs
10 ’ canine ’
11 >>> d . name # unique to d
12 ’ Fido ’
13 >>> e . name # unique to e
14 ’ Buddy ’

But the tricks list in the following code should not be used as a class

variable because just a single list would be shared by all instances of Dog:
1 c l a s s Dog :
2 tricks = [] # mistaken use o f a c l a s s v a r i a b l e
3 def init ( s e l f , name ) :
4 s e l f . name = name
5 def add trick ( s e l f , t r i c k ) :
6 s e l f . t r i c k s . append ( t r i c k )
7

8 >>> d = Dog ( ’ Fido ’ )


9 >>> e = Dog ( ’ Buddy ’ )
10 >>> d . a d d t r i c k ( ’ r o l l o v e r ’ )
11 >>> e . a d d t r i c k ( ’ p l a y dead ’ )
12 >>> d . t r i c k s # u n e x p e c t e d l y s h a r e d by a l l dogs
13 [ ’ r o l l o v e r ’ , ’ p l a y dead ’ ]