On 10/24/2011 7:45 AM, Wayne Werner wrote:
On Sun, Oct 23, 2011 at 11:06 PM, Marc Tompkins <marc.tompk...@gmail.com <mailto:marc.tompk...@gmail.com>> wrote:

    Things to remember:
    -you can get a value from a method, but you can't assign to it:
        variable = object.method()
    but NOT
        object.method() = variable


As a slight aside, you _can_ assign to the method name:

object.method = variable     #object.method is now whatever variable was

I'm not aware of any valid reason to do this, that is to say I don't know of anything that you could do this way that you couldn't do another more readable/maintainable way.

In my Python Pipelines program I deliberately reassign instance methods. Example:

Given a (trivial) pipeline specification: "< foo.txt | > goo.txt"
meaning: "open foo.txt, open goo.txt, read one record at a time from foo.txt, write it to goo.txt, at end close both files." The specification parser creates an instance of ReadFile and an instance of WriteFile, It must then "connect" these two instances, such that the output of the first goes to the input of the second. The IMHO easiest way to do this: instance1.output = instance2.input where output and input are methods.

I've had other cases where the __init__method of a class determines some behaviors by assigning one of several methods to a particular method.

I've had yet other cases where I swap methods during the first call, such that subsequent calls have different behavior.

All of these could be done differently, but reassigning methods makes the most sense to me.

--
Bob Gailer
919-636-4239
Chapel Hill NC

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to