can i implement virtual functions in python ?

Alex Martelli aleax at aleax.it
Wed Oct 1 06:31:28 EDT 2003


Andrew Dalke wrote:
   ...
> The better solution is to create an object which you
> can guarantee is not present anywhere else.  For
> example, you could do
> 
> class _default: pass # guaranteed to be unique
> 
> f = getattr(x, "there", _default)
> if f is not _default:
>     conn = f()
>     ...
> 
> I don't consider that code all that clean.  It isn't obvious
> why the _default class is needed.  Compare that to my

I think the "guaranteed to be unique" comment SHOULD make it
pretty obvious (to Pythonistas advanced enough to grasp the
try/except/else construct that we both prefer).

> and it uses a standard construct instead of depending
> on a class creation to make an object which is guaranteed
> to be unique.

Actually if you object to the "class creation" (which IS also
a standard construct:-), use ANY mutable object instead, e.g.:

_default = []   # ANY arbitrary mutable object, just for uniqueness
f = getattr(x, 'there', _default)
# etc etc


> In any case, remember that I said it's a personal preference,

Yeah, that's probably what's rubbing on people's nerves -- there
is SUPPOSED TO BE "ideally only one obvious way".  Maybe it
would help if we were Dutch;-).


Alex





More information about the Python-list mailing list