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

Marc Tompkins marc.tompkins at gmail.com
Tue Oct 25 06:53:57 CEST 2011


On Mon, Oct 24, 2011 at 3:44 PM, bob gailer <bgailer at gmail.com> wrote:

>  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>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.
>

As Einstein said, "Everything should be made as simple as possible, but not
simpler."  I also reassign methods (it's not my first choice, but sometimes
it's the best way to do what I want to do) but I thought it would muddy the
waters if I brought it up.  I apologize for over-simplifying.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111024/ddaa2afe/attachment-0001.html>


More information about the Tutor mailing list