Skip to content

Instantly share code, notes, and snippets.

@mstrongdev
Created January 17, 2014 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mstrongdev/8469932 to your computer and use it in GitHub Desktop.
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
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'
@SAKryukov
Copy link

SAKryukov commented Jan 27, 2018

This is really great idea and implementation. Thank you for sharing.

However, I would recommend to improve it considerably.
First of all,

getattr(cls.__metaclass__, attr, None)

should better be replaced with

getattr(type(cls), attr) # note that redundant actual argument None is removed

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

@SAKryukov
Copy link

After all, this is the ultimate solution

GitHub repository

Thank you very much.
—SA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment