[Tutor] Simple Question On A Method (in subclass)

bob gailer bgailer at gmail.com
Tue Oct 25 00:44:51 CEST 2011


On 10/24/2011 7:45 AM, Wayne Werner wrote:
> On Sun, Oct 23, 2011 at 11:06 PM, Marc Tompkins 
> <marc.tompkins at gmail.com <mailto:marc.tompkins at 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111024/e910c621/attachment.html>


More information about the Tutor mailing list