Roshan Book

My Tech Notebook

Python tutorial 12.1- Classes Object


class Duck:   # Creation of Class
        def quack(self,x): # Creation of methods
            if self.x==”jhilam”:
                print(‘{} is depressive ‘.format(self.x))
            elif self.x==”rita”:
                print(‘{} , I Love You ‘.format(self.x))
            else: print(‘{} is Normal’.format(self.x))
        def walk(self,y):
            print(‘{} Walk’.format(self.y))

i = input(“Enter your name t”)




self is passed, means name donald is passed.It is the first argument of method. It happens due to dot operator.

def __init__(self,value):  #This is constructor

This value can be used inside class saving in a local variable self.v=value . This is called encapsulation

def __init__(self,value):


Practical use of encapsulation 

Here you are passing any variable inside class and saving it in a dictionary and retrieving it from dictionary


class Duck:
    def __init__(self, **kwargs):
    def quack(self):

    def walk(self):
        print(‘Walks like a duck.’)
    def set_variable(self,k,v):
    def get_variable(self,k):
        return self.variables.get(k,None)

def main():
    donald = Duck(feet=2)
    print (donald.get_variable(‘feet’))

if __name__ == “__main__”: main()


Methods in one class inheriting methods from other class


E.g. class Dog(Animal):  # Dog class inheriting parent class methods


class Animal:
    def talk(self):
        print(‘I have something to say’)
    def clothes (self):
        print(‘I have nice clother’)

class Duck(Animal): # Duck inherited Animal methods
    def __init__(self, **kwargs):
    def quack(self):

    def walk(self):
        print(‘Walks like a duck.’)
    def set_variable(self,k,v):
    def get_variable(self,k):
        return self.variables.get(k,None)

def main():
    donald = Duck(feet=2)

if __name__ == “__main__”: main()
#walk in child class override parent class
#to ingeritent from parent function just write super().walk(). And now both walk methods will work


# Polymorphism is using object of one class as if it is an object of another class

class Duck():
    def method1(self):

class Dog():
    def method1(self):
def main():

def in_the_forest(dog):

def in_the_pond(duck):

#Now if we pass duck in the forest, it gives back result of method1 of dog object#

in_the_pond(fido)# – now fido passed in the pond like duck and then fido method 1 is called

for o in (donald, figo):
# here we call all methods in both the objects

if __name__ == “__main__”: main()
#To use polymorphically common interface is reuired in both classes
# That is same methods in two classes
# Then define a for loop and call method of the objects

# any function that is present in the object can be run inside a different function expecting that object and its method regarding of what type of object it is.

# Generator object

# Overriding a class

# our own range object which will inclusive of last value
# we will override the initial propoerty of range class of excluding last item

class inclusive_range:
    def __init__(self, args):
        if numargs<1:raise TypeError(‘require at  least one argument’)
        elif numargs==1:
        elif numargs==2:
        elif numargs==3:
        else: raise TypeError(‘expected at least 3 arguments, got {}’.format(numargs))
    def __iter__(self): # makes object an iterable objetc
        while i<=self.stop:
            yield i
            i+=self.step # This makes it an iterator


def main():
        for i in o:print(i, end=”)
if __name__==”__main__”:main()


# Decorators – functions that return other functions. Modify the way function works

you can set functions of same name with different methods

Inside a class

@propertydef color(self):‘’color’,None)=c


def color(self,c):[‘color’]=c


def color(self):


now you can just create the object donald=Duck()

and without passing the color=’blue ’ inside duck you can just fix as donald.color=’blue’ (uses @properties)

print (“donald.color”)   This ultimately print the color from color.setter

8 responses to “Python tutorial 12.1- Classes Object

  1. Latrice Maria October 25, 2011 at 6:47 pm

    After reading your blog post I browsed your website a bit and noticed you aren’t ranking nearly as well in Google as you could be. I possess a handful of blogs myself and I think you should take a look here: You’ll find it’s a very nice tool that can bring you a lot more visitors. Keep up the quality posts

  2. LV Luggage December 1, 2011 at 3:08 pm

    This is my first time i visit here. I discovered so a lot of entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I’m not the only 1 having all the enjoyment here! Maintain up the very good work.

  3. Gucci Handbags Outlet December 5, 2011 at 7:59 am

    This is truly an awesome bit of reading, not many blogs compare to the amount of clear info given away here for free!

  4. Cheap Designer Handbags December 5, 2011 at 10:04 am

    Incredibly nice layout and wonderful topic material , practically nothing else we want

  5. LV Purse Outlet December 5, 2011 at 3:15 pm

    Wow! This might be 1 particular of probably the most useful blogs We have ever arrive across on this topic. Basically Superb.

  6. Louis Vuitton Knockoffs Cheap December 5, 2011 at 3:53 pm

    Hello guys, wonderful web page, particularly rich in content and appropriately carefully thought out, personally I found here substantially interesting and useful

  7. Discount Chanel Bags December 6, 2011 at 6:35 am

    This post about condo gives the light in which we can observe the reality. this is particularly nice 1 and provides in depth specifics. thanks for this superior write-up Great post..

  8. Louis Vuitton Knockoffs Handbags December 6, 2011 at 10:34 am

    Simply wish to say your write-up is as amazing. The clarity within your post is simply spectacular and i could assume you are an expert on this topic.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: