Main Content
Python - a truly dynamic language Archive - Originally posted on "The Horse's Mouth" - 2009-01-30 17:18:13 - Graham Ellis
In Python everything is an object - and that includes functions which are objects which contain blocks of code. And this means that you can define different functions of the same name depending on a condition, and you can replace a function with another one too, if you want to.
This really is valid:
ontop = int(raw_input("Enter 1 if summat ontop "))
if ontop == 1:
def tanksize(basedim):
vlume = basedim * (basedim+1) * (basedim+2)
return vlume
else:
def tanksize(basedim):
vlume = basedim * (basedim+2)
return vlume
for k in range(10):
more = tanksize(k)
print "more is ", more
def tanksize(basedim):
vlume = basedim * (basedim+4)
return vlume
for k in range(10):
more = tanksize(k)
print "more is now", more
Would you write code like that? Probably not - but you could well write a piece of code that embeds within another application as required, and overrides default (preloaded) code. A number of commercial applications use Python for their tailoring, and this is the mechanism they often use to provide hooks for extra code for the minority of users who want to change certain algorithms.
Illustration - delegates write a program using the topics I have just covered in my lecture on a programming course (this course on site in Germany)
Some other articles
Y199 - Python - Fast Start Equality (in Python) A reminder of the key issues to consider in moving from Python 2 to Python 3 Some gems from Intermediate Python Some gems from an introduction to Python Python - current versions and implementations (CPython, Jython, IronPython etc) This article Y106 - Object Oriented Python When to check an object type - Python isinstance example Why populate object with values as you construct them? From single block to structure and object oriented programming What is the difference between a function and a method? Simple OO demonstration in C++, comparison to Python Really Simple Class and Inheritance example in Python Spike solution, refactored and reusable, Python - Example this or self - what are they, and what is the difference? (Python) From Structured to Object Oriented Programming. Object oriented or structured - a comparison in Python. Also writing clean regular expressions Moving from scripting to Object Orientation in Python From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) Object Oriented Programming for Structured Programmers - conversion training Tips for writing a test program (Ruby / Python / Java) When should I use OO techniques? This article Introduction to Object Oriented Programming Screw it or Glue it? Access to Object variables - a warning Python class rattling around Python - function v method Python makes University Challenge Class, static and unbound variables Y050 - Python - General A reminder of the key issues to consider in moving from Python 2 to Python 3 Identifying the first and last records in a sequence Well House Consultants - Python courses / what's special. Public training courses - upcoming dates Additional Python courses added to our schedule A longer Python ... training course Using Python to analyse last years forum logs. Good coding practise discussion. Whether you have programmed before or not, we can teach you Python How well do you know Perl and / or Python? Python Programming class for delegates who have already self-taught the basics Shell - Grep - Sed - Awk - Perl - Python - which to use when? Want to escape the Olympics? Learn to program in the countryside! When you should use Object Orientation even in a short program - Python example Python - current versions and implementations (CPython, Jython, IronPython etc) Python courses and Private courses - gently updating our product to keep it ahead of the game Busy weekend of contrasts. Python through the Snow Python training courses for use with ESRI ArcMap software Learning to program in Python 2 ... and / or in Python 3 Learning to program in ... Two days of demonstration scripts in Python Learning to program - how to jump the first hurdles Great new diagrams for our notes ... Python releases Learning PHP, Ruby, Lua and Python - upcoming courses Learning Python - many new example programs This article Python training