Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python Crash Course Classes 3 rd year Bachelors V1.0 dd 03-09-2013 Hour 7.

Similar presentations


Presentation on theme: "Python Crash Course Classes 3 rd year Bachelors V1.0 dd 03-09-2013 Hour 7."— Presentation transcript:

1 Python Crash Course Classes 3 rd year Bachelors V1.0 dd 03-09-2013 Hour 7

2 Introduction to language - methods >>> a = [2, 5, 3, 6, 5] >>> a.sort() >>> print(a) [2, 3, 5, 5, 6] >>> a.count(3) 1 >>> a.count(5) 2 >>> a.reverse() >>> print(a) [6, 5, 5, 3, 2] >>> d = {‘black’: 100, ‘grey’: 50, ‘white’: 0} >>> d.values() [0, 50, 100] >>> s = ‘-‘.join((‘2009’, ‘07’, ‘07’)) >>> print(s) 2009-07-07

3 Introduction to language - classes >>> class MyClass(object):... def __init__(self, x, y=1.0)... self.my_x = x... self.my_y = y...... def product():... return x*y... >>> m = MyClass(2, 4) >>> m.product() 8 >>> p = MyClass(5) >>> p.product() 5 >>> m.product() 8

4 Classes OOP terminology Class: A user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation. Class variable: A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class's methods. Class variables aren't used as frequently as instance variables are. Data member: A class variable or instance variable that holds data associated with a class and its objects. Function overloading: The assignment of more than one behavior to a particular function. The operation performed varies by the types of objects (arguments) involved. Instance variable: A variable that is defined inside a method and belongs only to the current instance of a class. Inheritance : The transfer of the characteristics of a class to other classes that are derived from it. Instance: An individual object of a certain class. An object obj that belongs to a class Circle, for example, is an instance of the class Circle. Instantiation : The creation of an instance of a class. Method : A special kind of function that is defined in a class definition. Object : A unique instance of a data structure that's defined by its class. An object comprises both data members (class variables and instance variables) and methods. Operator overloading: The assignment of more than one function to a particular operator.

5 Classes class Planet(object): 'Common base class for all planets' PlanetCount = 0 def __init__(self, name, mass): self.mass = mass self.name= name Planet.PlanetCount += 1 def displayCount(self): print "Total Planets %d" % Planet.PlanetCount def displayPlanet(self): print "Name : ", self.name, ", Mass: ", self.mass "This would create first object of Planet class" planet1 = Planet(“Earth", 5.972E24) "This would create second object of Planet class" planet2 = Planet(“Venus", 4.867E24)

6 Classes planet1.displayPlanet() Name: Earth, Mass: 5.972e+24 planet2.displayPlanet() Name: Venus, Mass: 4.867e+24 planet2.displayCount() Total Planets 2 planet1.satellites = 1 planet2.satellites = 0 del planet2.satellites hasattr(planet1, ‘satellites') # Returns true if 'satellites' attribute exists getattr(planet1, ‘satellites') # Returns value of 'satellites' attribute setattr(planet1, ‘satellites', 8) # Set attribute 'satellites' at 8 delattr(planetl, ‘satellites') # Delete attribute 'satellites'

7 Classes Built-in attributes Every Python class keeps following built-in attributes and they can be accessed using dot operator like any other attribute: __dict__ : Dictionary containing the class's namespace. __doc__ : Class documentation string, or None if undefined. __name__: Class name. __module__: Module name in which the class is defined. This attribute is "__main__" in interactive mode. __bases__ : A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list. Planet.__doc__: Common base class for all Planets Planet.__name__: Planet Planet.__module__: __main__ Planet.__bases__: () Planet.__dict__: {'__module__': '__main__', 'displayCount':, 'PlanetCount': 2, 'displayPlanet':, '__doc__': 'Common base class for all Planets', '__init__': }

8 Classes Inheritance #!/usr/bin/python class Parent: # define parent class parentAttr = 100 def __init__(self): print "Calling parent constructor" def parentMethod(self): print 'Calling parent method' def setAttr(self, attr): Parent.parentAttr = attr def getAttr(self): print "Parent attribute :", Parent.parentAttr class Child(Parent): # define child class def __init__(self): print "Calling child constructor" def childMethod(self): print 'Calling child method' c = Child() # instance of child c.childMethod() # child calls its method c.parentMethod()# calls parent's method c.setAttr(200) # again call parent's method c.getAttr() # again call parent's method Calling child constructor Calling child method Calling parent method Parent attribute : 200

9 Classes Overriding methods #!/usr/bin/python class Parent: # define parent class def myMethod(self): print 'Calling parent method' class Child(Parent): # define child class def myMethod(self): print 'Calling child method‘ Parent.myMethod(self) c = Child() # instance of child c.myMethod() # child calls overridden method Calling child method Calling parent method You can always override your parent class methods. One reason for overriding parent's methods is because you may want special or different functionality in your subclass.

10 Classes Overloading Methods SNMethod, Description & Sample Call 1 __init__ ( self [,args...] ) Constructor (with any optional arguments) Sample Call : obj = className(args) 2 __del__( self ) Destructor, deletes an object Sample Call : dell obj 3 __repr__( self ) Evaluatable string representation Sample Call : repr(obj) 4 __str__( self ) Printable string representation Sample Call : str(obj) 5 __cmp__ ( self, x ) Object comparison Sample Call : cmp(obj, x) #!/usr/bin/python class Vector: def __init__(self, a, b): self.a = a self.b = b def __str__(self): return 'Vector (%d, %d)' % (self.a, self.b) def __add__(self,other): return Vector(self.a+other.a,self.b+other.b) v1 = Vector(2,10) v2 = Vector(5,-2) print v1 + v2 Vector(7,8)

11 Classes Data hiding #!/usr/bin/python class JustCounter: __secretCount = 0 def count(self): self.__secretCount += 1 print self.__secretCount counter = JustCounter() counter.count() print counter.__secretCount 1 2 Traceback (most recent call last): File "test.py", line 12, in print counter.__secretCount AttributeError: JustCounter instance has no attribute '__secretCount' An object's attributes may or may not be visible outside the class definition. For these cases, you can name attributes with a double underscore prefix, and those attributes will not be directly visible to outsiders:

12 Introduction to language End


Download ppt "Python Crash Course Classes 3 rd year Bachelors V1.0 dd 03-09-2013 Hour 7."

Similar presentations


Ads by Google