On Sep 27, 2006, at 9:03 AM, Alan Bourke wrote:
Everything, and I mean everything, in Python is an object.
Basic types like int aren't, surely?
Surely they are! Strings, ints, booleans - all objects. A class
definition? That's an object, too. A function or an object's method?
Objects, too.
This is incredibly useful once you get your brain around it. Let's
say that you have an object, and want to tell another object to do
something, and when it's done, to let the original object know that
it's complete. This is commonly called a 'callback'. In Python, you
would pass a reference to the callback function directly to the
target, and it would then invoke that function reference. The code
would look something like this (keep in mind that 'def' is equivalent
to Fox's 'PROC'):
class MainObject(object):
def initiateAction(self):
# Assume that 'target' is a reference to the TargetObject
# class already established
self.target.doSomeAction(self.notification)
def notification(self, result):
# This is the callback function
print "Process complete, result:", result
class TargetObject(object):
def doSomeAction(self, callback):
# Run some length process, and then pass the
# result to the callback
longProcess()
anotherLongProcess()
ret = finalLongProcess()
# Here's the callback
callback(ret)
Note that in the last line, 'callback' is being called as a
function, even though it was passed as a parameter. Note that the
name of the callback method doesn't matter, nor does the object to
which it belongs, if any.
This is just the tip of the iceberg. Once you get used to thinking
along these lines, it makes designing complex interactions much
simpler, especially for asynchronous events. It also makes class
factories ridiculously easy to write; since a class definition is an
object, there is nothing to stop you from subclassing from it on the
fly - after all, you're just creating a new class object!
-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com
_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/profox
OT-free version of this list: http://leafe.com/mailman/listinfo/profoxtech
** All postings, unless explicitly stated otherwise, are the opinions of the
author, and do not constitute legal or medical advice. This statement is added
to the messages for those lawyers who are too stupid to see the obvious.