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 396,901 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 3,992 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:

Starting Python

Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,406
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Solution Re: Starting Python

  #27  
Aug 24th, 2005
A look at a simple Python class example for the OOP beginner ...
  1. # looking at a simple Python class
  2. # a class combines a number of methods/functions that belong together
  3.  
  4. class Box(object):
  5. """class names by convention are capitalized, (object) is an optional inheritance"""
  6. def __init__(self, depth, height, width):
  7. """__init__() is called first (acts as Constructor)
  8. brings in data from outside the class and makes it
  9. usable within the class via the instance object self.
  10. Look at self as the data carrier within the class.
  11. """
  12. self.depth = depth
  13. self.height = height
  14. self.width = width
  15.  
  16. def volume(self):
  17. return self.height * self.width * self.depth
  18.  
  19. def surface(self):
  20. return 2*self.height*self.width + 2*self.height*self.depth + 2*self.width*self.depth
  21.  
  22. # construct an instance of a 10x10x10 box and reference it with box1
  23. box1 = Box(10, 10 ,10)
  24. print "A 10x10x10 box has a volume of", box1.volume()
  25. print "and a surface area of", box1.surface()
  26.  
  27. print
  28.  
  29. print "Let's change the depth to 5"
  30. # construct an instance of a 5x10x10 box and reference it with box2
  31. box2 = Box(5, 10 ,10)
  32. print "A 5x10x10 box has a volume of", box2.volume()
  33. print "and a surface area of", box2.surface()
  34.  
  35. print
  36.  
  37. print "Optional tests for the inquisitive folks:"
  38. print "box1 =", box1
  39. print "box2 =", box2
  40. print "The depth of box2 is", box2.depth
  41.  
  42. print
  43.  
  44. # Can we set the box dimensions directly? Let's try it.
  45. box1.depth = 5
  46. box1.height = 5
  47. box1.width = 5
  48. print "Box1 volume after setting box1 dimensions to 5x5x5 =", box1.volume()
Here class Box inherits the most basic container class object (actually just a place holder), this is the newer class writing convention, adding (object) is "still" optional.
Last edited by vegaseat : Mar 1st, 2007 at 2:46 pm. Reason: [code=python] tag
May 'the Google' be with you!
Reply With Quote  
All times are GMT -4. The time now is 4:37 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC