0% found this document useful (0 votes)
6 views

python objects

Class objects in Python support attribute references and instantiation, allowing access to attributes defined in the class's namespace. Instantiation creates a new instance of the class and can invoke an __init__() method for initialization. Instance objects can have data attributes and methods, with methods being functions that belong to the instance.

Uploaded by

mdhasan.ansari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

python objects

Class objects in Python support attribute references and instantiation, allowing access to attributes defined in the class's namespace. Instantiation creates a new instance of the class and can invoke an __init__() method for initialization. Instance objects can have data attributes and methods, with methods being functions that belong to the instance.

Uploaded by

mdhasan.ansari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Class Objects

Class objects support two kinds of operations: attribute references and instantiation.

Attribute references use the standard syntax used for all attribute references in
Python: obj.name. Valid attribute names are all the names that were in the class’s
namespace when the class object was created. So, if the class definition looked like
this:

class MyClass:
"""A simple example class"""
i = 12345

def f(self):
return 'hello world'

then MyClass.i and MyClass.f are valid attribute references, returning an integer and a
function object, respectively. Class attributes can also be assigned to, so you can
change the value of MyClass.i by assignment. __doc__ is also a valid attribute, return-
ing the docstring belonging to the class: "A simple example class".

Class instantiation uses function notation. Just pretend that the class object is a pa-
rameterless function that returns a new instance of the class. For example (assuming
the above class):

x = MyClass()

creates a new instance of the class and assigns this object to the local variable x.

The instantiation operation (“calling” a class object) creates an empty object. Many
classes like to create objects with instances customized to a specific initial state.
Therefore a class may define a special method named __init__(), like this:

def __init__(self):
self.data = []

When a class defines an __init__() method, class instantiation automatically in-


vokes __init__() for the newly created class instance. So in this example, a new, ini-
tialized instance can be obtained by:

x = MyClass()

Of course, the __init__() method may have arguments for greater flexibility. In that
case, arguments given to the class instantiation operator are passed on to __init__().
For example,

>>>

>>> class Complex:


... def __init__(self, realpart, imagpart):
... self.r = realpart
... self.i = imagpart
...
>>> x = Complex(3.0, -4.5)
>>> x.r, x.i
(3.0, -4.5)
9.3.3. Instance Objects

Now what can we do with instance objects? The only operations understood by in-
stance objects are attribute references. There are two kinds of valid attribute names:
data attributes and methods.

data attributes correspond to “instance variables” in Smalltalk, and to “data mem-


bers” in C++. Data attributes need not be declared; like local variables, they spring
into existence when they are first assigned to. For example, if x is the instance of My-
Class created above, the following piece of code will print the value 16, without leav-
ing a trace:

x.counter = 1
while x.counter < 10:
x.counter = x.counter * 2
print(x.counter)
del x.counter

The other kind of instance attribute reference is a method. A method is a function that
“belongs to” an object.

Valid method names of an instance object depend on its class. By definition, all at-
tributes of a class that are function objects define corresponding methods of its in-
stances. So in our example, x.f is a valid method reference, since MyClass.f is a func-
tion, but x.i is not, since MyClass.i is not. But x.f is not the same thing as My-
Class.f — it is a method object, not a function object.

You might also like