[Python-Dev] Info needed on metaclasses (original) (raw)
Dimitris Garanatsios rs96057@hotmail.com
Sun, 24 Mar 2002 11:07:57 +0200
- Previous message: [Python-Dev] ����g-�_���`�S�f����
- Next message: [Python-Dev] Info needed on metaclasses / type-ing
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
This is a multi-part message in MIME format.
------=_NextPart_000_0006_01C1D324.277B7410 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit
I recently browsed through an unfinished book of Bruce Eckel, "Thinking in Python", Revision 0.1.2 I can't remember the URL where i got it, but i found it through searching for documentation on Python from the main site, www.python.org I found the following peace of code there:
#: c01:SingletonMetaClass.py
class SingletonMetaClass(type):
def init(cls, name, bases, dict):
super(SingletonMetaClass, cls).init(name, bases, dict)
original_new = cls.new
def my_new(cls, *args, **kwds):
if cls.instance == None:
cls.instance = original_new(cls, *args, **kwds)
return cls.instance
cls.instance = None
cls.new = staticmethod(my_new)
class bar(object):
metaclass = SingletonMetaClass
def init(self,val):
self.val = val
def str(self):
return self
+ self.val
...
#:~
My question is about the "SingletonMetaClass" class. I have tried in the past to create my own types using Python and just could not find a way of doing that since built-in type objects' structure where not documented.
The "SingletonMetaClass though", as far as i can understand, please help me with that, is doing exactly what i wanted: a new type definition. I browsed Python's manuals to find more about the attributes and functions that are used, but found nothing...
Could anyone help me find information about the meaning and use of the following attributes/functions/objects? These are:
--- the "metaclass" attribute of the "bar" class (derived from object)
--- the "object" object itself
--- the "super" function used in the "SingletonMetaClass" class (a class for a new type object?)
--- the "new" attribute/method of a class (applies to any class?)
--- the "staticmethod" function used in the "SingletonMetaClass" class
--- any additional info about related classes or functions that are not used in the above example but exist and are available to use
Thanx, Dimitris
------=_NextPart_000_0006_01C1D324.277B7410 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable
I recently browsed = through an unfinished book of Bruce Eckel, = "Thinking in Python", Revision 0.1.2
I can't remember the = URL where i got it, but i found it through searching for documentation on Python from the main site, www.python.org
_
_I found the following = peace of code there:
_
_-------------------------------------------= -----------------------
= ; =
#: c01:SingletonMetaClass.py
class SingletonMetaClass(type):<= /p>
def __init__(cls, name, bases, dict):
= super(SingletonMetaClass, cls).__init__(name, bases, dict)
= original_new =3D cls.__new__
= def my_new(cls, *args, **kwds):
&nbs= p; if cls.instance =3D=3D = None:
&nbs= p; cls.instance =3D original_new(cls, *args, **kwds)
&nbs= p; return cls.instance
= cls.instance =3D None
= cls.__new__ =3D staticmethod(my_new)
class bar(object):
__metaclass__ =3D SingletonMetaClass
def __init__(self,val):
= self.val =3D val
def __str__(self):
= return `self` + self.val
...
#:~
-------------------------------------------= -----------------------
_
_My question is about = the "SingletonMetaClass" = class.
I have tried in the = past to create my own types using Python and just could not find a way of doing = that since built-in type objects' structure where not = documented.
_
_The "SingletonMetaClass though", as far as i can understand, = please help me with that, is doing exactly what i = wanted: a new type definition. I browsed Python's manuals to find more about the = attributes and functions that are used, but found = nothing...
_
_Could anyone help me = find information about the meaning and use of the following attributes/functions/objects? These are:
_
_--- the "__metaclass__" attribute of the "bar" class (derived from object)
_
_--- the "object" object itself
_
_--- the "super" function used in the "SingletonMetaClass" class (a class for a new type object?)
_
_--- the "__new__" attribute/method of a class (applies to any = class?)
_
_--- the "staticmethod" function used in = the "SingletonMetaClass" = class
_
_--- any additional info about related classes or functions that are not used in = the above example but exist and are available to = use
_
_Thanx, Dimitris_
__
_------=_NextPart_000_0006_01C1D324.277B7410--
- Previous message: [Python-Dev] ����g-�_���`�S�f����
- Next message: [Python-Dev] Info needed on metaclasses / type-ing
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]