object.enable() anti-pattern

Greg Ewing greg.ewing at canterbury.ac.nz
Sun May 12 19:18:10 EDT 2013


Wayne Werner wrote:
> On Fri, 10 May 2013, Gregory Ewing wrote:
> 
>>   f = open("myfile.dat")
>>   f.close()
>>   data = f.read()
> 
> To clarify - you don't want a class that has functions that need to be 
> called in a certain order with *valid input* in order to not crash.
> 
> Exactly what does happen - a ValueError is raised because you're(*) 
> passing self into the file.read() function, and that input is invalid

The same argument can be applied to:

    foo = Foo()
    foo.do_something()
    foo.enable() # should have done this first

You're passing an invalid input to Foo.do_something,
namely a Foo that hasn't been enabled yet.

-- 
Greg



More information about the Python-list mailing list