User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 402,506 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,841 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Aug 11th, 2005
Views: 17,152
Python is entirely object oriented and using classes is made relatively simple. Beginners have a certain angst when it comes to using classes. There is a hump in the learning curve, which I like to overcome with this example. Inheritance really makes sense and saves you a lot of extra code writing. The best way to learn about classes and object oriented programming is to write more and more of your code with class!
python Syntax | 5 stars
  1. # a simple example of a class inheritance
  2. # tested with Python24 vegaseat 10aug2005
  3.  
  4. help('object') # test
  5.  
  6. class Class1(object):
  7. """
  8. Class1 inherits the most basic container class object (just a place holder)
  9. this is the newer class writing convention, adding (object) is "still" optional
  10. """
  11.  
  12. k = 7
  13.  
  14. def __init__(self, color='green'):
  15. """
  16. Special method __init__() is called first (acts as Constructor).
  17. It brings in data from outside the class like the variable color.
  18. (in this case color is also set to a default value of green)
  19. The first parameter of any method/function in the class is always self,
  20. the name self is used by convention. Assigning color to self.color allows it
  21. to be passed to all methods within the class. Think of self as a carrier,
  22. or if you want impress folks call it target instance object.
  23. The variable k is assigned a value in the class, but outside of the methods.
  24. You can access k in a method using self.k
  25. """
  26. self.color = color
  27.  
  28. def Hello1(self):
  29. print "Hello from Class1!"
  30.  
  31. def printColor(self):
  32. """in this case self allows color to be passed"""
  33. print "I like the color", self.color
  34.  
  35. def __localHello(self):
  36. """
  37. A variable or function with a double underline prefix and no or max. single
  38. underline postfix is considered private to the class and is not inherited or
  39. accessible outside the class.
  40. """
  41. print "A hardy Hello only used within the class!"
  42.  
  43.  
  44. class Class2(Class1):
  45. """
  46. Class2 inherits Class1 (Class2 is the subclass, Class1 the base or superclass)
  47. Class1 has to be coded before Class2 for this to work!!!
  48. Class2 can now use any method of Class1, and even the variable k
  49. """
  50.  
  51. def Hello2(self):
  52. print "Hello from Class2!"
  53. print self.k, "is my favorite number"
  54.  
  55.  
  56. # the color blue is passed to __init__()
  57. c1 = Class1('blue')
  58.  
  59. # Class2 inherited method __init__() from Class1
  60. # if you used c2 = Class2(), the default color green would be picked
  61. c2 = Class2('red')
  62.  
  63. print '-'*20
  64. print "Class1 says hello:"
  65. c1.Hello1()
  66.  
  67. print '-'*20
  68. print "Class2 says a Class1 hello:"
  69. c2.Hello1()
  70.  
  71. print '-'*20
  72. print "Class2 says its own hello:"
  73. c2.Hello2()
  74.  
  75. print '-'*20
  76. print "Class1 color via __init__():"
  77. c1.printColor()
  78.  
  79. print '-'*20
  80. print "Class2 color via inherited __init__() and printColor():"
  81. c2.printColor()
  82.  
  83. print '-'*20
  84. print "Class1 changes its mind about the color:"
  85. c1 = Class1('yellow') # same as: c1.__init__('yellow')
  86. c1.printColor()
  87.  
  88. print '-'*20
  89. print "Wonder what Class2 has to say now:"
  90. c2.printColor()
  91.  
  92. print '-'*20
  93. # this would give an error! Class1 does not have a method Hello2()
  94. if hasattr(Class1, "Hello2"):
  95. print c1.Hello2()
  96. else:
  97. print "Class1 does not contain method Hello2()"
  98.  
  99. # check inheritance
  100. if issubclass(Class2, Class1):
  101. print "Class2 is a subclass of Class1, or Class2 has inherited Class1"
  102.  
  103. # you can access variable k contained in Class1
  104. print "Variable k from Class1 =", c1.k
  105.  
  106. print '-'*20
  107. # this would give an error! You cannot access a class private method
  108. if hasattr(Class1, "__localHello()"):
  109. print c1.__localHello()
  110. else:
  111. print "No access to Class1 private method __localHello()"
Comments (Newest First)
edacval | Newbie Poster | Feb 13th, 2008
You still can access __localHello() as _Class1__localHello()
vegaseat | Kickbutt Moderator | Aug 24th, 2005
Python has a leg up over Java. Python can have multiple inheritance, just separate the inherited classes with a comma.
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 5:49 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC