| |||||||||||
| |||||||||||
all variables are objects
Python is a truly object oriented language, where every variable is an object, and every piece of code is a method. That means that even a named block of code is an object, held in a named variable and that even an operator like "+" is really just a shorthand for a method call - __add__ in this case. The result is a very efficient language which, once you learn how to use it, has a flexibility beyond your dreams. Here's a sample piece of code in which we're defining a class of clothing ... adding clothes together adds the weight of each piece of clothing, and combines the colours as a string adding in the word "and" in between - a typical use of an overloaded operator in Python. """Demonstration that in Python ... all operations are methods all variables (including code) are objects all blocks are defined using insets. """ class clothing: """ A simple class with a constructor and two property accessors. We also redefine addition and the equality test for clothing """ def __init__(self,colour,weight): self.colour=colour self.weight=weight def getcolour(self): return self.colour def getweight(self): return self.weight def __add__(self,second): result = clothing(self.colour,self.weight) result.weight += second.weight result.colour += " and " + second.colour return result def __eq__(first,second): if first.weight == second.weight and \ first.colour == second.colour: return 1 return 0 # Test code - to be run only if this file is # run standalone if __name__ == "__main__": wearing = [] wearing.append(clothing("blue",1.0)) wearing.append(clothing("brown",1.25)) # Following lines both run the __add__ method outfit = wearing[0] + wearing[1] wearing.append(outfit) wearing.append(outfit.__add__(wearing[1])) # Following lines show that methods are objects print wearing.append print wearing[0].getcolour for haveon in wearing: print "I am wearing something coloured",haveon.getcolour(), print "And weighing",haveon.getweight(),"kgs" Here's what you'll see when you run that code [localhost:~/ipy] graham% python address <built-in method append of list object at 0x3c38a0> <bound method clothing.getcolour of <__main__.clothing instance at 0x40a580>> I am wearing something coloured blue And weighing 1.0 kgs I am wearing something coloured brown And weighing 1.25 kgs I am wearing something coloured blue and brown And weighing 2.25 kgs I am wearing something coloured blue and brown and brown And weighing 3.5 kgs [localhost:~/ipy] graham% See also Object Orientation in Python Please note that articles in this section of our
web site were current and correct to the best of our ability when published,
but by the nature of our business may go out of date quite quickly. The
quoting of a price, contract term or any other information in this area of
our website is NOT an offer to supply now on those terms - please check
back via our main web site
Related Material
Object Oriented Python Python - Objects - Intermediate resource index - Python Solutions centre home page You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum. At Well House Consultants, we provide training courses on subjects such as Ruby, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site. | |||||||||||
PH: 0800 043 8225 or 01225 708225 • FAX: 0845 8382 405 or 01225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho | |||||||||||