Monday, 15 February 2010

python - Inheriting methods from a metaclass -


The example encoding given in

has been reproduced below, implemented in TOKEN Why does __ include __ and __ repr __ to metaclass enumeration type ? Ctypes import * class EnumerationType (type (c_uint) from the

 : Def __new__ (metacls, name, bases, dict): If not, then for the "_members_" key: _members_ = {} For the key, dict.items () value: if not key.startswith ("_"): _members_ [key] = key ["_ members_"] = _members_cls = type (c_uint) .___ key (metacples, name , Sockets, dict) for values, values ​​in cls_members_.items (): globals () [key] = return values ​​CLD DIF __ particles __ (self, value): return value in self. _members_.values ​​() def __repr __ (self): return "& lt; counting% s>" % Self.__name__ class Enumeration (c_uint): __metaclass__ = EnumerationType _members_ = {} def __init __ (self, value): for k, self._members_.items () in itself: if v == value: self.name = K break Other: value increment (no counting member with "value% r") c_uint .__ init __ (self, value) @ paramamethod def -param (CLS, Ultimate): ifinstance (Ultimate, calculation): If the ultimate .__ class__! = Cls: Increase ValueError ("Counting members can not be mixed") and: return the ultimate and return: class (absolute) def __repr __ (self): return "& lt; Member% s =% d of% r & Gt; " % (Self.name, self.value, self.__ class__) class token (enumeration): _members_ = {'T_UNDEF': 0, 'T_NAME': 1, 'T_NUMBER': 2, 't_string': 3, 'T_OPERATOR I would expect to throw the effect by the following code which is included in  __  

Code> In spite of this, I get the True False .

  print in token, 7 in tokens  

enumeration and TOKEN are EnumerationType :

  & gt; & Gt; & Gt; Isinstance (counting, enumeration type) true & gt; & Gt; & Gt; Eustace (token, enumeration type) is true  

and, e.g. repr (TOKEN) is equal to type (token) .__ repr __ (TOKEN) , which is the enumerationType .__ repr __ (TOKEN) .


No comments:

Post a Comment