Instance and Methods in Class - Object Oriented Programming in Python

Instance and Methods in Class - Object Oriented Programming in Python

Instance and Methods in Python Object Oriented Programming

We learnt in Last blog Class and Objects - Object Oriented Programming in Python how to create Class and objects we learnt about constructors.

But after learning instant objects, what to do with them?

The only operations understood by instance objects are attribute references. There are two kinds of valid attribute names: data attributes and methods.

Data Attributes

Data attributes refer to instance variables.

Data attributes need not be declared; like local variables, they spring into existence when they are first assigned to.

For eg:

class class2:
    pass
abc=class2()
abc.name="Saurabh Jadhav"
print(abc.name)

if abc is the instance of class2 created above, the following piece of code will print the value Saurabh Jadhav.

The other kind of instance attribute reference is a method. 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 methods as well. For example, list objects have methods called append, insert, remove, sort, and so on. However, in the following discussion, we’ll use the term method exclusively to mean methods of class instance objects, unless explicitly stated otherwise.)

Method Objects

Lets take example of class we used in classes and Objects:

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

    def f(self):
        return 'hello world'

The way we call function is :

just after creating Object like this ,

x=MyClass()

Function Call:

x.f()

In the MyClass example, this will return the string 'hello world'. However, it is not necessary to call a method right away: x.f is a method object, and can be stored away and called at a later time. For example:

xf = x.f
while True:
    print(xf())

will continue to print hello world until the end of time.

What exactly happens when a method is called? You may have noticed that x.f() was called without an argument above, even though the function definition for f() specified an argument. What happened to the argument? Surely Python raises an exception when a function that requires an argument is called without any — even if the argument isn’t actually used…

Actually, you may have guessed the answer: the special thing about methods is that the instance object is passed as the first argument of the function. In our example, the call x.f() is exactly equivalent to MyClass.f(x).

Code Examples and Concepts

Generally speaking, instance variables are for data unique to each instance and class variables are for attributes and methods shared by all instances of the class:

class Dog:

    kind = 'canine'         # class variable shared by all instances

    def __init__(self, name):
        self.name = name    # instance variable unique to each instance

>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.kind                  # shared by all dogs
'canine'
>>> e.kind                  # shared by all dogs
'canine'
>>> d.name                  # unique to d
'Fido'
>>> e.name                  # unique to e
'Buddy'

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 Dog instances:

class Dog:

    tricks = []             # mistaken use of a class variable

    def __init__(self, name):
        self.name = name

    def add_trick(self, trick):
        self.tricks.append(trick)

>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.add_trick('roll over')
>>> e.add_trick('play dead')
>>> d.tricks                # unexpectedly shared by all dogs
['roll over', 'play dead']

Correct design of the class should use an instance variable instead:

class Dog:

    def __init__(self, name):
        self.name = name
        self.tricks = []    # creates a new empty list for each dog

    def add_trick(self, trick):
        self.tricks.append(trick)

>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.add_trick('roll over')
>>> e.add_trick('play dead')
>>> d.tricks
['roll over']
>>> e.tricks
['play dead']

Updated Value :

If the same attribute name occurs in both an instance and in a class, then attribute lookup prioritizes the instance:

>>> class Warehouse:
        purpose = 'storage'
        region = 'west'

>>> w1 = Warehouse()
>>> print(w1.purpose, w1.region)
storage west
>>> w2 = Warehouse()
>>> w2.region = 'east'
>>> print(w2.purpose, w2.region)
storage east

Often, the first argument of a method is called self. This is nothing more than a convention: the name self has absolutely no special meaning to Python. Note, however, that by not following the convention your code may be less readable to other Python programmers, and it is also conceivable that a class browser program might be written that relies upon such a convention.

Calling Methods in another methods:

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

    def add(self, x):
        self.data.append(x)

    def addtwice(self, x):
        self.add(x)
        self.add(x)

You can call other methods in same class.

So that was about it, we learnt deeply about instances and methods in classes of Python, In next blog we will learn fundamental Concepts of Object Oriented Programming.

Did you find this article valuable?

Support saurabh jadhav by becoming a sponsor. Any amount is appreciated!