Python Tutorial ( PDFDrive )
pt1 = Point()pt2 = pt1pt3 = pt1print id(pt1), id(pt2), id(pt3) # prints the ids of the obejctsdel pt1del pt2del pt3When the above code is executed, it produces the following result:3083401324 3083401324 3083401324Point destroyedNote: Ideally, you should define your classes in separate file, then you should import them in your main programfile using import statement. Kindly check Python - Modules chapter for more details on importing modulesand classes.Class Inheritance: Instead of starting from scratch, you can create a class by deriving it from a preexisting class by listing the parentclass in parentheses after the new class name.The child class inherits the attributes of its parent class, and you can use those attributes as if they were definedin the child class. A child class can also override data members and methods from the parent.SYNTAX: Derived classes are declared much like their parent class; however, a list of base classes to inherit from are givenafter the class name:class SubClassName (ParentClass1[, ParentClass2, ...]):'Optional class documentation string'class_suiteEXAMPLE: #!/usr/bin/pythonclass Parent: # define parent classparentAttr = 100def __init__(self):print "Calling parent constructor"def parentMethod(self):print 'Calling parent method'def setAttr(self, attr):Parent.parentAttr = attrdef getAttr(self):print "Parent attribute :", Parent.parentAttrclass Child(Parent): # define child classdef __init__(self):print "Calling child constructor"def childMethod(self):TUTORIALS POINT Simply Easy Learning
print 'Calling child method'c = Child()c.childMethod()c.parentMethod()c.setAttr(200)c.getAttr()# instance of child# child calls its method# calls parent's method# again call parent's method# again call parent's methodWhen the above code is executed, it produces the following result:Calling child constructorCalling child methodCalling parent methodParent attribute : 200Similar way, you can drive a class from multiple parent classes as follows:class A:.....class B:.....# define your class A# define your calss Bclass C(A, B): # subclass of A and B.....You can use issubclass() or isinstance() functions to check a relationships of two classes and instances.• The issubclass(sub, sup) boolean function returns true if the given subclass sub is indeed a subclass of thesuperclass sup.• The isinstance(obj, Class) boolean function returns true if obj is an instance of class Class or is an instanceof a subclass of ClassOverriding Methods: You can always override your parent class methods. One reason for overriding parent's methods is because youmay want special or different functionality in your subclass.EXAMPLE: #!/usr/bin/pythonclass Parent: # define parent classdef myMethod(self):print 'Calling parent method'class Child(Parent): # define child classdef myMethod(self):print 'Calling child method'c = Child()c.myMethod()# instance of child# child calls overridden methodWhen the above code is executed, it produces the following result:Calling child methodTUTORIALS POINT Simply Easy Learning
- Page 263 and 264: # Open a filefd = os.open( "foo.txt
- Page 265 and 266: print "Current working dir : %s" %
- Page 267 and 268: path = "/var/www/html/foo.txt"fd =
- Page 269 and 270: Example The following example shows
- Page 271 and 272: _vti_txtrobots.txtitemlistingresume
- Page 273 and 274: # Close opened fileos.close( fd )#
- Page 275 and 276: dev_num = os.makedev(major_dnum, mi
- Page 277 and 278: Return Value This method does not
- Page 279 and 280: -rw-------. 1 root root 0 Apr 30 02
- Page 281 and 282: os.pathconf(path, name) Descriptio
- Page 283 and 284: Description The method popen() open
- Page 285 and 286: # !/usr/bin/pythonimport ossrc = '/
- Page 287 and 288: Syntax Following is the syntax for
- Page 289 and 290: Return Value This method does not
- Page 291 and 292: # stat informationstatinfo = os.sta
- Page 293 and 294: Description The method tcgetpgrp()
- Page 295 and 296: os.tempnam(dir, prefix)Parameters
- Page 297 and 298: Description The method ttyname() re
- Page 299 and 300: Return Value This method does not
- Page 301 and 302: Return Value This method returns t
- Page 303 and 304: KeyErrorNameErrorUnboundLocalErrorE
- Page 305 and 306: ......................else:If there
- Page 307 and 308: print "Error: can\'t find file or r
- Page 309 and 310: self.args = argSo once you defined
- Page 311 and 312: 'Optional class documentation strin
- Page 313: For the above class let's try to ac
- Page 317 and 318: Data Hiding: An object's attribute
- Page 319 and 320: groups()This method returns all mat
- Page 321 and 322: SYNTAX: re.sub(pattern, repl, strin
- Page 323 and 324: \s Matches whitespace. Equivalent t
- Page 325 and 326: Backreferences: This matches a prev
- Page 327 and 328: CHAPTER20Python CGI ProgrammingWhat
- Page 329 and 330: If you click hello.py, then this pr
- Page 331 and 332: Simple URL Example : Get Metho
- Page 333 and 334: </form>The result of this code is t
- Page 335 and 336: text_content = form.getvalue('textc
- Page 337 and 338: Here is an example of how to retrie
- Page 339 and 340: CHAPTER21Python Database AccessThe
- Page 341 and 342: EXAMPLE: Following is the example o
- Page 343 and 344: ('Mac', 'Mohan', 20, 'M', 2000)try:
- Page 345 and 346: DELETE Operation: DELETE operation
- Page 347 and 348: ProgrammingErrorSubclass of Databas
- Page 349 and 350: The socket Module: To create a so
- Page 351 and 352: Protocol Common function Port No Py
- Page 353 and 354: Subject: SMTP e-mail testThis is a
- Page 355 and 356: try:smtpObj = smtplib.SMTP('localho
- Page 357 and 358: count = 0while count < 5:time.sleep
- Page 359 and 360: Synchronizing Threads: The threadi
- Page 361 and 362: thread = myThread(threadID, tName,
- Page 363 and 364: <stars>10</stars><description>Talk
print 'Calling child method'
c = Child()
c.childMethod()
c.parentMethod()
c.setAttr(200)
c.getAttr()
# instance of child
# child calls its method
# calls parent's method
# again call parent's method
# again call parent's method
When the above code is executed, it produces the following result:
Calling child constructor
Calling child method
Calling parent method
Parent attribute : 200
Similar way, you can drive a class from multiple parent classes as follows:
class A:
.....
class B:
.....
# define your class A
# define your calss B
class C(A, B): # subclass of A and B
.....
You can use issubclass() or isinstance() functions to check a relationships of two classes and instances.
• The issubclass(sub, sup) boolean function returns true if the given subclass sub is indeed a subclass of the
superclass sup.
• The isinstance(obj, Class) boolean function returns true if obj is an instance of class Class or is an instance
of a subclass of Class
Overriding Methods:
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.
EXAMPLE:
#!/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'
c = Child()
c.myMethod()
# instance of child
# child calls overridden method
When the above code is executed, it produces the following result:
Calling child method
TUTORIALS POINT
Simply Easy Learning