Evaluating python - a question

Alex Martelli aleaxit at yahoo.com
Sun May 20 03:28:13 EDT 2001


"Nick Perkins" <nperkins7 at home.com> wrote in message
news:mXIN6.16348$eK2.3031270 at news4.rdc1.on.home.com...
> ..how about a module that can check whether an object has picked up
> any 'extra' attributes after a certain time?
>
> eg:
>
> def registe(obj): # save obj attibutes
>
> def check(obj): # compare atts to saved...
>
>
> ..you could 'register' your object when it is supposed to be fully
> initialized, and then 'check' it later on.
> ..or maybe it could key on the class instead of the instance,
> keeping a list of the 'proper' attributes for members of a class.

If you like this approach, you can get "instant diagnosis" of any
errant attribute-setting by overriding __setattr__ instead.  I.e.,
in debug-mode,

def registe(obj):
    class Watchdog1(obj.__class__):
        Base = obj.__class__
        def __setattr__(obj, name, value):
            if noattr(obj, name, Base):
                raise AttributeError, name
            doset(obj, name, Base)
    obj.__class__ = Watchdog1()

for appropriate functions 'noattr' (must return true if the attr
can't be set -- e.g., by checking in obj.__dict__ if it's already
there, or checking the class, or whatever) and doset (must do
the setting -- in obj.__dict__, or via Base's __setattr__, etc).


Alex






More information about the Python-list mailing list