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,
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,