Behavioural identity - a short discussion
Terry Reedy
tjreedy at udel.edu
Sun Apr 10 23:34:51 EDT 2005
"Kay Schluehr" <kay.schluehr at gmx.net> wrote in message
news:1113165441.978711.64860 at z14g2000cwz.googlegroups.com...
> class A(object):
> def __init__(self):
> raise NotImplemented
>
> We can regard class A as a "pure abstract" class. It is impossible to
> create instances of A.
You see that parameter 'self'? When __init__ is called, it is bound to an
already created instance of A. How soon the last reference goes away I am
not sure. If you change the raise statement to 'raise
SomeException(self)', then that instance will become bound to the 'args'
attribute of the resulting exception object. That exception object can in
turn be get captured by an 'except SomeException, obj' statement. The
binding of 'obj' persists after the except statement.
To abort object creation, write a metaclass with __new__ and raise an
exception there.
> Thinking in Python makes live easier because we can not only
> check interfaces superficially but we can inspect the code and we can
> compare two code-objects on the behavioural level with a certain
> accuracy.
You are actually thinking in terms of the CPython implementation, not
Python itself, which is fine as long as you don't expect CPython-specific
introspection to work with other implementations. But it is pretty neat
sometimes.
Terry J. Reedy
More information about the Python-list
mailing list