Metaclass/abc hackery

D

Demian Brecht

As with most I'm sure, short of using abc's, I've had very little exposure
to metaclasses. So, when digging into abc implementation, I figured it
would be a good idea to dig into metaclasses, their usage and actually try
writing one.

What I did may be contrived, but it was fun nonetheless and a good
introduction to how metaclasses can be used (of course, they should only be
used when absolutely required and no other solution is readily available
due to the black magic that happens under the hood): I ended up writing a
proof of concept that's abc-like in nature. However, it doesn't depend on
inheritance. It allows you to say: "I want to make sure that this object
/look/ like this type when instantiated".

Again, simple proof of concept that has holes in it and is likely
altogether a bad idea, but it was fun to throw together nonetheless, so I
thought I'd share: https://gist.github.com/demianbrecht/6944269 (check out
the tests at the bottom for usage).

Working on this though brought up a question: Is there anything in the
data model that acts like "__setattr__" but when operating on a class
definition instead of an instance? I'd be able to get rid of the late_bind
function if something like that's available... Not likely something that
would be used very often, but would likely sometimes be useful.

Thanks,
 
M

Marco Buttu

Working on this though brought up a question: Is there anything in the
data model that acts like "__setattr__" but when operating on a class
definition instead of an instance? I'd be able to get rid of the
late_bind function if something like that's available... Not likely
something that would be used very often, but would likely sometimes be
useful.

Thanks,

I am not sure about your question, but I try to explain a bit some
possibilities. If you define a __setattr__ method in the metaclass, then
you can intercept the attribute assignments only after class creation:
.... def __setattr__(cls, name, value):
.... print("in __setattr__(): ", name, value)
.... super().__setattr__(name, value)
........ a = 33

As you can see, the above code does not print the message. But after
class creation it does:
in __setattr__(): a 33

This because during the class creation there is any class yet, so it is
not possible to intercept argument assignment by __setattr__.
If you want to intercept the assignments during class creation too, you
can intercept the class attribute dictionary assignment. In this case
you can just write a dictionary object that overrides __setitem__, and
then by overriding the __prepare__ metaclass method in order to return
this dictionary:
.... def __setitem__(self, name, value):
.... print('In Namespace.__setitem__():', name, value)
.... super().__setitem__(name, value)
........ def __prepare__(clsname, bases):
.... return Namespace()
.... def __setattr__(cls, name, value):
.... print("In MetaFoo.__setattr__(): ", name, value)
.... super().__setattr__(name, value)
........ a = 33
....
In Namespace.__setitem__(): __module__ __main__
In Namespace.__setitem__(): __qualname__ Foo
In Namespace.__setitem__(): a 33In MetaFoo.__setattr__(): a 33

Of course, it is not a so good solution, because if you need to manage
in the same way either the attributes before or after the class
creation, you have to do it by writing some code outside the methods:
.... print('do something with', name, value)
........ def __setitem__(self, name, value):
.... print('In Namespace.__setitem__():', name, value)
.... manage(name, value)
.... super().__setitem__(name, value)
........ def __prepare__(clsname, bases):
.... return Namespace()
.... def __setattr__(cls, name, value):
.... print("In MetaFoo.__setattr__(): ", name, value)
.... manage(name, value)
.... super().__setattr__(name, value)
........ a = 33
....
In Namespace.__setitem__(): __module__ __main__
do something with __module__ __main__
In Namespace.__setitem__(): __qualname__ Foo
do something with __qualname__ Foo
In Namespace.__setitem__(): a 33
do something with a 33In MetaFoo.__setattr__(): a 33
do something with a 33
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top