metaclass

__metaclass__ in Python 3

__metaclass__ in Python 3 Question: In Python2.7 this code can work very well, __getattr__ in MetaTable will run. But in Python 3 it doesn’t work. class MetaTable(type): def __getattr__(cls, key): temp = key.split(“__”) name = temp[0] alias = None if len(temp) > 1: alias = temp[1] return cls(name, alias) class Table(object): __metaclass__ = MetaTable def …

Total answers: 1

Why doesn't the namedtuple module use a metaclass to create nt class objects?

Why doesn't the namedtuple module use a metaclass to create nt class objects? Question: I spent some time investigating the collections.namedtuple module a few weeks ago. The module uses a factory function which populates the dynamic data (the name of the new namedtuple class, and the class attribute names) into a very large string. Then …

Total answers: 4

How are Python metaclasses different from regular class inheritance?

How are Python metaclasses different from regular class inheritance? Question: This might be too much of an open ended question, but I’m just now learning about metaclasses in Python and I don’t understand how a metaclass is any different than just having a child class inherit from the parent class, like class child(parent): Wouldn’t this …

Total answers: 2

Python Metaclass : Understanding the 'with_metaclass()'

Python Metaclass : Understanding the 'with_metaclass()' Question: I want to ask what the with_metaclass() call means in the definition of a class. E.g.: class Foo(with_metaclass(Cls1, Cls2)): Is it a special case where a class inherits from a metaclass? Is the new class a metaclass, too? Asked By: Zakos || Source Answers: with_metaclass() is a utility …

Total answers: 2

Understanding metaclass and inheritance in Python

Understanding metaclass and inheritance in Python Question: I have some confusion regarding meta-classes. With inheritance class AttributeInitType(object): def __init__(self,**kwargs): for name, value in kwargs.items(): setattr(self, name, value) class Car(AttributeInitType): def __init__(self,**kwargs): super(Car, self).__init__(**kwargs) @property def description(self): return “%s %s %s %s” % (self.color, self.year, self.make, self.model) c = Car(make=’Toyota’, model=’Prius’, year=2005, color=’green’) print c.description With …

Total answers: 1

How to pass arguments to the metaclass from the class definition?

How to pass arguments to the metaclass from the class definition? Question: I’m trying to dynamically generate classes in python 2.7, and am wondering if you can easily pass arguments to the metaclass from the class object. I’ve read this post, which is awesome, but doesn’t quite answer the question. at the moment I am …

Total answers: 2

How to wrap every method of a class?

How to wrap every method of a class? Question: I’d like to wrap every method of a particular class in python, and I’d like to do so by editing the code of the class minimally. How should I go about this? Asked By: rjkaplan || Source Answers: You mean programatically set a wrapper to methods …

Total answers: 3

Overriding __contains__ method for a class

Overriding __contains__ method for a class Question: I need to simulate enums in Python, and did it by writing classes like: class Spam(Enum): k = 3 EGGS = 0 HAM = 1 BAKEDBEANS = 2 Now I’d like to test if some constant is a valid choice for a particular Enum-derived class, with the following …

Total answers: 2

Using the __call__ method of a metaclass instead of __new__?

Using the __call__ method of a metaclass instead of __new__? Question: When discussing metaclasses, the docs state: You can of course also override other class methods (or add new methods); for example defining a custom __call__() method in the metaclass allows custom behavior when the class is called, e.g. not always creating a new instance. …

Total answers: 6