object.enable() anti-pattern

Wayne Werner wayne at waynewerner.com
Sun May 12 13:14:33 EDT 2013


On Fri, 10 May 2013, Gregory Ewing wrote:

> Wayne Werner wrote:
>> You don't ever want a class that has functions that need to be called in a 
>> certain order to *not* crash.
>
> That seems like an overly broad statement. What
> do you think the following should do?
>
>   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 
input - specifically:

     ValueError: I/O operation on closed file

*where you actually means python, because when you call 
`your_instance.method()`, it works effectively like a call to 
`YourClass.method(your_instance)`

-W



More information about the Python-list mailing list