Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
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
  [477] - ()
  [834] - ()
  [900] - ()
  [1306] - ()
  [1348] - ()
  [1925] - ()
  [2017] - ()
  [2169] - ()
  [2604] - ()
  [3085] - ()
  [3399] - ()
  [3436] - ()
  [3673] - ()
  [3878] - ()
  [3947] - ()
  [4021] - ()
  [4028] - ()
  [4129] - ()
  [4448] - ()
  [4591] - ()
  [4650] - ()
  [4721] - ()

Python - Objects - Intermediate
  [296] - ()
  [383] - ()
  [477] - ()
  [656] - ()
  [831] - ()
  [903] - ()
  [964] - ()
  [1146] - ()
  [1217] - ()
  [1517] - ()
  [1644] - ()
  [1661] - ()
  [1819] - ()
  [2368] - ()
  [2409] - ()
  [2485] - ()
  [2693] - ()
  [2717] - ()
  [2720] - ()
  [2722] - ()
  [2764] - ()
  [2785] - ()
  [2889] - ()
  [2905] - ()
  [2994] - ()
  [3002] - ()
  [3442] - ()
  [3472] - ()
  [3524] - ()
  [3796] - ()
  [3887] - ()
  [4028] - ()
  [4094] - ()
  [4344] - ()
  [4356] - ()
  [4366] - ()
  [4410] - ()
  [4449] - ()
  [4450] - ()
  [4541] - ()
  [4649] - ()
  [4717] - ()
  [4718] - ()
  [4719] - ()

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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/solutions/python-a ... jects.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard