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
Object Oriented Python [3436] Moving from scripting to Object Orientation in Python - (2011-09-13)
[3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
[3085] Object Oriented Programming for Structured Programmers - conversion training - (2010-12-14)
[2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
[2169] When should I use OO techniques? - (2009-05-11)
[2017] Python - a truly dynamic language - (2009-01-30)
[1925] Introduction to Object Oriented Programming - (2008-12-06)
[1348] Screw it or Glue it? Access to Object variables - a warning - (2007-09-12)
[1306] Python class rattling around - (2007-08-16)
[900] Python - function v method - (2006-10-20)
[834] Python makes University Challenge - (2006-08-15)
[477] Class, static and unbound variables - (2005-10-25)
Python - Objects - Intermediate [3524] Metaclasses (Python) and Metatables (Lua) - (2011-11-17)
[3472] Static variables in functions - and better ways using objects - (2011-10-10)
[3442] A demonstration of how many Python facilities work together - (2011-09-16)
[3002] A list of special method and attribute names in Python - (2010-10-17)
[2994] Python - some common questions answered in code examples - (2010-10-10)
[2905] Defining static methods in Python - (2010-08-05)
[2889] Should Python classes each be in their own file? - (2010-07-27)
[2785] The Light bulb moment when people see how Object Orientation works in real use - (2010-05-28)
[2764] Python decorators - your own, staticmethod and classmethod - (2010-05-14)
[2722] Mixins example in Python - (2010-04-14)
[2720] Multiple inheritance in Python - complete working example - (2010-04-14)
[2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
[2693] Methods that run on classes (static methods) in Python - (2010-03-25)
[2485] How do I set up a constant in Python? - (2009-10-31)
[2409] TypeError: super() argument 1 must be type, not classobj (Python) - (2009-09-18)
[2368] Python - fresh examples of all the fundamentals - (2009-08-20)
[1819] Calling base class constructors - (2008-10-03)
[1661] Equality, sameness and identity - Python - (2008-05-31)
[1644] Using a utility method to construct objects of different types - Python - (2008-05-17)
[1517] Python - formatting objects - (2008-01-24)
[1217] What are factory and singleton classes? - (2007-06-04)
[1146] __new__ v __init__ - python constructor alternatives? - (2007-04-14)
[964] Practical polymorphism in action - (2006-12-04)
[903] Pieces of Python - (2006-10-23)
[831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
[656] Think about your design even if you don't use full UML - (2006-03-24)
[477] Class, static and unbound variables - (2005-10-25)
[383] Overloading of operators on standard objects in Python - (2005-07-19)
[296] Using a Python dictionary as a holder of object attributes - (2005-04-30)
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, Lua, 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.