Re: Metaclasses and classproperties

2019-09-10 Thread Eko palypse
Thank you for your thoughts. I'm trying to migrating en existing python2 api, which has been build using boost::python with pure python3 code. All of the py2 classes do have these additional properties names and values. You are right, using dict(Ordinal.__members__) for names could have been used

Re: Metaclasses and classproperties

2019-09-10 Thread Peter Otten
Eko palypse wrote: > I'm fairly new when it comes to metaclass programming and therefore the > question whether the following makes sense or not. > > The goal is to have two additional class properties which return a > dictionary name:class_attribute and value:class_attribute for an IntEnum > cla

Re: Metaclasses - magic functions

2016-12-23 Thread Mr. Wrobel
W dniu 23.12.2016 o 15:14, Ian Kelly pisze: (...) cls.added_in_init = 'test' Man, you are awsome genius! Finally somebody was able to explain me what is the power of __new__ and difference between __init__ !!! So what I wanted to achieve was adding some new attributes to the class ins

Re: Metaclasses - magic functions

2016-12-23 Thread Ian Kelly
On Fri, Dec 23, 2016 at 5:14 AM, Mr. Wrobel wrote: > Hi,thanx for answers, let's imagine that we want to add one class attribute > for newly created classess with using __init__ in metaclass, here's an > example: > > #!/usr/bin/env python > > class MetaClass(type): > # __init__ manipulation: >

Re: Metaclasses - magic functions

2016-12-23 Thread Mr. Wrobel
W dniu 21.12.2016 o 02:51, Ethan Furman pisze: On 12/20/2016 03:39 PM, Ben Finney wrote: "Mr. Wrobel" writes: Quick question, can anybody tell me when to use __init__ instead of __new__ in meta programming? Use ‘__new__’ to do the work of *creating* one instance from nothing; allocating the

Re: Metaclasses - magic functions

2016-12-20 Thread Ethan Furman
On 12/20/2016 03:39 PM, Ben Finney wrote: "Mr. Wrobel" writes: Quick question, can anybody tell me when to use __init__ instead of __new__ in meta programming? Use ‘__new__’ to do the work of *creating* one instance from nothing; allocating the storage, determining the type, etc. — anything

Re: Metaclasses - magic functions

2016-12-20 Thread Ethan Furman
On 12/20/2016 03:26 PM, Ian Kelly wrote: ... The latter is interesting mostly because it allows you to set the __slots__ or do something interesting with the __prepare__ hook (although the only interesting thing I've ever seen done with __prepare__ is to use an OrderedDict to preserve the the de

Re: Metaclasses - magic functions

2016-12-20 Thread Ben Finney
"Mr. Wrobel" writes: > Quick question, can anybody tell me when to use __init__ instead of > __new__ in meta programming? Use ‘__new__’ to do the work of *creating* one instance from nothing; allocating the storage, determining the type, etc. — anything that will be *the same* for every instance

Re: Metaclasses - magic functions

2016-12-20 Thread Ian Kelly
On Tue, Dec 20, 2016 at 2:04 PM, Mr. Wrobel wrote: > Hi, > > Quick question, can anybody tell me when to use __init__ instead of __new__ > in meta programming? > > I see that __new__ can be used mostly when I want to manipulate with class > variables that are stored into dictionary. > > But when t

Re: metaclasses

2008-03-04 Thread castironpi
On Mar 4, 12:51 am, Gerard Flanagan <[EMAIL PROTECTED]> wrote: > On Mar 4, 6:31 am, [EMAIL PROTECTED] wrote: > > > > > > > On Mar 3, 10:01 pm, Benjamin <[EMAIL PROTECTED]> wrote: > > > > On Mar 3, 7:12 pm, [EMAIL PROTECTED] wrote: > > > > > What are metaclasses? > > > > Depends on whether you want

Re: metaclasses

2008-03-03 Thread Gerard Flanagan
On Mar 4, 6:31 am, [EMAIL PROTECTED] wrote: > On Mar 3, 10:01 pm, Benjamin <[EMAIL PROTECTED]> wrote: > > > > > On Mar 3, 7:12 pm, [EMAIL PROTECTED] wrote: > > > > What are metaclasses? > > > Depends on whether you want to be confused or not. If you do, look at > > this old but still head bursting

Re: metaclasses

2008-03-03 Thread castironpi
On Mar 3, 10:01 pm, Benjamin <[EMAIL PROTECTED]> wrote: > On Mar 3, 7:12 pm, [EMAIL PROTECTED] wrote: > > > What are metaclasses? > > Depends on whether you want to be confused or not. If you do, look at > this old but still head bursting > essay:http://www.python.org/doc/essays/metaclasses/. > >

Re: metaclasses

2008-03-03 Thread Benjamin
On Mar 3, 7:12 pm, [EMAIL PROTECTED] wrote: > What are metaclasses? Depends on whether you want to be confused or not. If you do, look at this old but still head bursting essay: http://www.python.org/doc/essays/metaclasses/. Basically, the metaclass of a (new-style) class is responsible for crea

Re: metaclasses

2008-03-03 Thread castironpi
On Mar 3, 8:22 pm, "Daniel Fetchinson" <[EMAIL PROTECTED]> wrote: > > What are metaclasses? > > http://www.google.com/search?q=python+metaclass > > HTH, > Daniel Not satisfied. http://en.wikipedia.org/wiki/Metaclass#Python_example That's a limitation. The constructor can omit the superclass cal

Re: metaclasses

2008-03-03 Thread Daniel Fetchinson
> What are metaclasses? http://www.google.com/search?q=python+metaclass HTH, Daniel -- http://mail.python.org/mailman/listinfo/python-list

Re: metaclasses: timestamping instances

2007-09-03 Thread Gabriel Genellina
En Mon, 03 Sep 2007 07:39:05 -0300, km <[EMAIL PROTECTED]> escribi�: > But why does it show varied difference in the time between a and b > instance creations when __metaclass__ hook is used and when not used in > class Y ? I dont understand that point ! What do you expect from a._created, b.

Re: metaclasses: timestamping instances

2007-09-03 Thread km
Hi, But why does it show varied difference in the time between a and b instance creations when __metaclass__ hook is used and when not used in class Y ? I dont understand that point ! KM On 9/1/07, Michele Simionato <[EMAIL PROTECTED]> wrote: > > On Sep 1, 6:07 pm, Steve Holden <[EMAIL PROTECTE

Re: metaclasses: timestamping instances

2007-09-01 Thread Michele Simionato
On Sep 1, 6:07 pm, Steve Holden <[EMAIL PROTECTED]> wrote: > > Debugging with Wing IDE and examining the classes at a breakpoint shows > this to be true (even after Y's __metaclass__ assignment is commented out): > > >>> X.__metaclass__ > > >>> Y.__metaclass__ > > >>> For the benefit of the

Re: metaclasses: timestamping instances

2007-09-01 Thread Steve Holden
km wrote: > Hi all, > > I have extended a prototype idea from Alex Martelli's resource on > metaclasses regarding time stamping of instances. > > > import time > class Meta(type): > start = time.time() > def __call__(cls, *args, **kw): > print 'Meta start time %e'%cls.start >

Re: metaclasses and performance

2007-06-21 Thread Mirko Dziadzka
Lenard Lindstrom <[EMAIL PROTECTED]> wrote: > I don't know if C asserts are active in release Python, but for > new-style classes one thing that happens during attribute lookup is that > an object's class is asserted to be an instance of type. Thank's for the explanation. My Linux distribution

Re: metaclasses and performance

2007-06-19 Thread Lenard Lindstrom
Mirko Dziadzka wrote: > Hi all > > I'm playing around with metaclasses and noticed, that there is small > but mesurable a performance difference in the code shown below. With a > more complex example I get a 5 percent performance penalty for using a > metaclass. Until today I assumed, that a meta

Re: metaclasses (beginner question)

2007-02-22 Thread James Stroud
Peter Otten wrote: > You determine the factory Python uses to > make a class by adding > > __metaclass__ = factory > > to the class body, so you'll probably end with something like > > class ProducerHandlerType(type): > # your code > > class A: > __metaclass__ = ProducerHandlerType >

Re: metaclasses (beginner question)

2007-02-21 Thread Laszlo Nagy
> Without looking into the details -- the (subclass of) type is meant to be > the class of the class, or the other way round, your normal classes are > instances of (a subclass of) type. You determine the factory Python uses to > make a class by adding > > __metaclass__ = factory > > to the class

Re: metaclasses (beginner question)

2007-02-21 Thread Peter Otten
Laszlo Nagy wrote: > I would like to create a hierarchy classes, where the leaves have a > special attribute called "producer_id". In addition, I would like to > have a function that can give me back the class assigned to any > producer_id value. I tried to implement this with a metaclass, but I >

Re: Metaclasses are not called in subclasses. What did I wrong?

2006-10-29 Thread Georg Brandl
Létező wrote: > I use Python 2.4.4. Please read the code below: > > --- > from new import classobj > > def mymeta(name,bases,clsdict): > print 'meta: %s'%name > return classobj(name,bases,clsdict) > > class A(object): > __metacl

Re: Metaclasses are not called in subclasses. What did I wrong?

2006-10-29 Thread Peter Otten
Létez? wrote: > I use Python 2.4.4. Please read the code below: > > --- > from new import classobj > > def mymeta(name,bases,clsdict): > print 'meta: %s'%name > return classobj(name,bases,clsdict) mymeta is not a class. > class A

Re: Metaclasses, decorators, and synchronization

2005-09-27 Thread Michael Ekstrand
On Tuesday 27 September 2005 00:22, Michele Simionato wrote: > It is not that easy, but you can leverage on my decorator module > which does exactly what you want: > http://www.phyast.pitt.edu/~micheles/python/decorator.zip Excellent. Thank you :-). - Michael -- http://mail.python.org/mailman/li

Re: Metaclasses, decorators, and synchronization

2005-09-26 Thread Michele Simionato
Michael Ekstrand ha scritto: > One issue remains in this function: my method's signature is lost when > synchronized is applied (at least as somemeth=synchronized(somemeth); > I'm currently in a 2.3 environment that doesn't have the decorator > syntax, but my understanding is that makes no differe

Re: Metaclasses, decorators, and synchronization

2005-09-26 Thread Michael Ekstrand
On Sep 26, 2005, at 4:21 PM, Scott David Daniels wrote: > Unnecessarily holding a lock while acquiring another can be a nasty > source of deadlock or at least delay. Another source of problems is > holding a lock because an exception skipped past the release code. I had thought of part of that af

Re: Metaclasses, decorators, and synchronization

2005-09-26 Thread Scott David Daniels
Michael Ekstrand wrote: > Something like this (not yet tested): > > import threading > global_lock = threading.Lock() > def synchronized(meth): > def inner(self, *args, **kwargs): > try: > self._sync_lock.acquire() > except AttributeError: > global_lock.

Re: Metaclasses, decorators, and synchronization

2005-09-26 Thread Michael Ekstrand
On Sep 26, 2005, at 2:16 PM, Tom Anderson wrote: > You could define a meta-lock, and use that to protect the > lock-installation action. Something like this (not yet tested): import threading global_lock = threading.Lock() def synchronized(meth): def inner(self, *args, **kwargs):

Re: Metaclasses, decorators, and synchronization

2005-09-26 Thread Tom Anderson
On Mon, 26 Sep 2005, Jp Calderone wrote: > On Sun, 25 Sep 2005 23:30:21 -0400, Victor Ng <[EMAIL PROTECTED]> wrote: >> You could do it with a metaclass, but I think that's probably overkill. >> >> It's not really efficient as it's doing test/set of an RLock all the >> time, but hey - you didn't a

Re: Metaclasses, decorators, and synchronization

2005-09-25 Thread Victor Ng
Hmmm well that's obvious enough.  This is why I shouldn't write code off the cuff on c.l.p :)OTOH - if I just assign the RLock in the base classes initializer, is there any problem?vic On 9/26/05, Jp Calderone <[EMAIL PROTECTED]> wrote: On Sun, 25 Sep 2005 23:30:21 -0400, Victor Ng <[EMAIL

Re: Metaclasses, decorators, and synchronization

2005-09-25 Thread Jp Calderone
On Sun, 25 Sep 2005 23:30:21 -0400, Victor Ng <[EMAIL PROTECTED]> wrote: >You could do it with a metaclass, but I think that's probably overkill. > >It's not really efficient as it's doing test/set of an RLock all the >time, but hey - you didn't ask for efficient. :) There's a race condition in t

Re: Metaclasses, decorators, and synchronization

2005-09-25 Thread Michael Ekstrand
On Sunday 25 September 2005 22:30, Victor Ng wrote: > You could do it with a metaclass, but I think that's probably > overkill. OK. And thanks for the example :-). It looks simple enough... I didn't think the solution would be overly complex. And the RLock makes it easier than I anticipated - wa

Re: Metaclasses, decorators, and synchronization

2005-09-25 Thread Victor Ng
You could do it with a metaclass, but I think that's probably overkill. It's not really efficient as it's doing test/set of an RLock all the time, but hey - you didn't ask for efficient. :) 1 import threading 2 3 def synchronized(func): 4 def innerMethod(self, *args,

Re: Metaclasses and new-style classes

2005-08-07 Thread Robert Kern
Jan-Ole Esleben wrote: > Hi! > > I've just posted a question about metaclasses in ZOPE on the ZOPE > list, and one of the replies said that metaclasses (at least > "painless" metaclasses) cannot be used without new-style classes (or > rather, that they don't work where you cannot explicitly use ne

Re: Metaclasses and new-style classes

2005-08-07 Thread Jordan Rastrick
Correct me if I'm wrong, but I think the OP was asking if metaclasses work with old-style classes, not new-style. [EMAIL PROTECTED] wrote: > This may be a limitation Zope imposes. > > I wrote this program: > #--- > class M(type):

Re: Metaclasses and new-style classes

2005-08-07 Thread jepler
This may be a limitation Zope imposes. I wrote this program: #--- class M(type): def __new__(*args): print "new M", args class T(object): __metaclass__ = M #

Re: Metaclasses and class variables

2005-08-04 Thread Bengt Richter
On Thu, 4 Aug 2005 17:53:28 +0200, Jan-Ole Esleben <[EMAIL PROTECTED]> wrote: >Thanks! It's a bit icky, yes, but I've been so wrapped up in >complicated thinking that I didn't see this. It's actually quite an >OK solution (I need it because I have an internal representation for >method interfaces

Re: Metaclasses and class variables

2005-08-04 Thread Jan-Ole Esleben
Thanks! It's a bit icky, yes, but I've been so wrapped up in complicated thinking that I didn't see this. It's actually quite an OK solution (I need it because I have an internal representation for method interfaces that needs to be saved somewhere without the user having to worry about it, and wi

Re: Metaclasses and class variables

2005-08-04 Thread Mike C. Fletcher
Jan-Ole Esleben wrote: >Yes, that works, but it is unfortunately not an option (at least not a >good one). > >Is there no way to create a class variable that exists during >definition of the class? (I cannot imagine there isn't, since >technically it's possible and manually it can be done...) > >O

Re: Metaclasses and class variables

2005-08-04 Thread Jan-Ole Esleben
Yes, that works, but it is unfortunately not an option (at least not a good one). Is there no way to create a class variable that exists during definition of the class? (I cannot imagine there isn't, since technically it's possible and manually it can be done...) Ole > classvar is defined AFTER

Re: Metaclasses and class variables

2005-08-04 Thread Kent Johnson
Christopher Subich wrote: > Jan-Ole Esleben wrote: > >> class Meta(type): >> def __new__(cls, name, bases, d): >> d['classvar'] = [] >> return type.__new__(cls, name, bases, d) > > > The problem is that __new__ is called upon object construction, not > class definition, but you're try

Re: Metaclasses and class variables

2005-08-04 Thread Thomas Heller
Jan-Ole Esleben <[EMAIL PROTECTED]> writes: > Hi! > > I am new to this list, and maybe this is a stupid question, but I > can't seem to find _any_ kind of answer anywhere. > > What I want to do is the following: > I want to insert a class variable into a class upon definition and > actually use it

Re: Metaclasses and class variables

2005-08-04 Thread Jan-Ole Esleben
I thought __new__ was called upon construction of the _class_ object that "Meta" is the type of. Then it would be available at the time of the definition of my class. Or am I mistaken? Ole 2005/8/4, Christopher Subich <[EMAIL PROTECTED]>: > Jan-Ole Esleben wrote: > > class Meta(type): > > def _

Re: Metaclasses and class variables

2005-08-04 Thread Christopher Subich
Jan-Ole Esleben wrote: > class Meta(type): > def __new__(cls, name, bases, d): > d['classvar'] = [] > return type.__new__(cls, name, bases, d) The problem is that __new__ is called upon object construction, not class definition, but you're trying to set the class variables at definitio

RE: Metaclasses

2004-12-23 Thread Robert Brewer
Bob Cowdery wrote: > Thanks for your help Robert. I'm still not > understanding what I am seeing. I've forgotten > about the objective for the moment. I just want > to understand metaclasses. All right; then I'll skip the speech about how metaclasses are not the solution you're looking for. ;) Th

RE: Metaclasses

2004-12-23 Thread Bob . Cowdery
Title: RE: Metaclasses Shalabh Yes I am realising there are variaous ways to achieve the same end. I guess I am in research mode at the moment and understanding what metaclasses can do is important even if I end up not using them. There is another thread on this question where I am trying

RE: Metaclasses

2004-12-23 Thread Bob . Cowdery
Title: RE: Metaclasses Robert Brewer wrote: >Okay. It depends on where you're getting that capability information from, but the simplest approach I can > >think of would be to stick it in the class: > >class API(object): >    __metaclass__ = MetaAPI >    > 

Re: Metaclasses

2004-12-22 Thread Shalabh Chaturvedi
[EMAIL PROTECTED] wrote: I am trying to build a capability based API. That is, an instance of the api will reflect the capabilities of some underlying services. The question I'd ask at this point is - does the term 'instance of the API' correspond to a Python class, or an instance of the class th

RE: Metaclasses

2004-12-22 Thread Robert Brewer
Bob Cowdery wrote: > What I want to do is when > > class API(object): > __metaclass__ = MetaAPI > > is created that MetaAPI generates attributes from a given > capability map and not the one it picked up on 'import'. Okay. It depends on where you're getting that capability information from,

RE: Metaclasses

2004-12-22 Thread Bob . Cowdery
Title: RE: Metaclasses Robert Yes you understand exectly what I am trying to do, my test code was not much more than you show. You are right that to do the simple thing of adding attributes I can do that in-line as it were but there may be other customisations that I want to do. I am

RE: Metaclasses

2004-12-22 Thread Robert Brewer
Bob Cowdery wrote: > I am trying to build a capability based API. That is, > an instance of the api will reflect the capabilities > of some underlying services. I could have several > different instances of the api concurrently running > against different back end services. A ui applet will > bin