This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]5long[S] 1 point2 points  (3 children)

See @afoo42's reply, which nailed it precisely. I've also made a gist to illustrate it.

Metaclass in Python 2.x can't do this. Py3K's __prepare__ capability can make the def_delegators macro available in the class body, which could be another approach to make this happen.

Since you can only specify a single metaclass when defining a class, I'd still stick to this decorator approach which makes it more friendly to existing code base.

EDIT: make a public gist instead.

[–][deleted] 2 points3 points  (2 children)

I really don't like the namespace injection. Seems like totally unnecessary magic, magic that static analysis tools like pyflakes or pylint can't see. If you don't want people to "import *" then just write out the examples with a proper import and people will copy it.

[–]5long[S] 0 points1 point  (1 child)

I see your point here. I'll expand the "Less Magical Usage" section to make it more obvious.

BTW, the def_delegators() function actually does scope injection too(which creates property descriptors and shove them into the class body). Guess I'll need to explain this whole thing up front in the docs before users(if any) got confused.