Created
January 17, 2014 08:05
-
-
Save mstrongdev/8469932 to your computer and use it in GitHub Desktop.
A Python metaclass that allows for easy creation of 'public' static 'read-only' properties by storing '_' prefixed members on the metaclass. Inspired by http://stackoverflow.com/q/1735434/1385101
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ROType(type): | |
def __new__(mcl, classname, bases, classdict): | |
class UniqueROType (mcl): | |
pass | |
def getAttrFromMetaclass(attr): | |
return lambda cls: getattr(cls.__metaclass__, attr, None) | |
for attr, value in classdict.items(): | |
if not hasattr(value, '__call__') and attr.startswith('_') and not attr.startswith('__'): | |
# Store the private value on the metaclass. | |
setattr(mcl, attr, value) | |
# Create a property whose getter queries the attr from the metaclass. | |
p = property(getAttrFromMetaclass(attr)) | |
# Add the property to our empty metaclass so the correct property | |
# behavior is generated on the class. | |
setattr(UniqueROType, attr[1:], p) | |
# Expose the new 'public' read-only property on the class. | |
classdict[attr[1:]] = p | |
# Remove the private value from the class. | |
classdict.pop(attr, None) | |
return type.__new__(UniqueROType, classname, bases, classdict) | |
# | |
# EXAMPLES | |
# | |
class Foo(object): | |
__metaclass__ = ROType | |
foo = 0 | |
_bar = 1 | |
_baz = 2 | |
class Test(object): | |
__metaclass__ = ROType | |
foo = 'hello' | |
_bar = 'world' | |
_baz = 'sam' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is really great idea and implementation. Thank you for sharing.
However, I would recommend to improve it considerably.
First of all,
should better be replaced with
Now, this is compatible with both Python 2 and 3.
The problem was: assigning the attribute
__metaclass__
is not the only (and, by far, not the best even for Python 2) way of setting a metaclass. Essentially,__metaclass__
is not really metaclass. A metaclass can be set by constructing a class directly from metaclass class:myClass = ROType("someClass", (), {}
.Another improvement could be avoiding this manipulations with underscore. Not many will like it. It would be much better to use user-supplied function for recognizing attributes to be converting to property and property naming; it could even be a simple rule-defined data parameter, such as dictionary.
Thank you again.
—SA