How to Adding Functionality to a Class by metaclass(not by inherit)
How to Adding Functionality to a Class by metaclass(not by inherit) #example: import inspect class Foo(object): def f(self): pass def g(self): pass class MetaFoo(type): def __init__(cls, name, bases, dic): super(MetaFoo, cls).__init__(name, bases, dic) for n, f in inspect.getmembers(Foo, inspect.ismethod): setattr(cls, n, f) #Bar want to achieve Foo's part/all functionality, but not by inherit class Bar(object): __metaclass__ = MetaFoo >>> b = Bar() >>> b.f() TypeError: unbound method f() must be called with Foo instance as first argument (got nothing instead) >>> Bar.f >>> b.f how can I set Bar.f as -- http://mail.python.org/mailman/listinfo/python-list
How to Adding Functionality to a Class by metaclass(not by inherit)
How to Adding Functionality to a Class by metaclass(not by inherit) #example: import inspect class Foo(object): def f(self): pass def g(self): pass class MetaFoo(type): def __init__(cls, name, bases, dic): super(MetaFoo, cls).__init__(name, bases, dic) for n, f in inspect.getmembers(Foo, inspect.ismethod): setattr(cls, n, f) #Bar want to achieve Foo's part/all functionality, but not by inherit class Bar(object): __metaclass__ = MetaFoo >>> b = Bar() >>> b.f() TypeError: unbound method f() must be called with Foo instance as first argument (got nothing instead) >>> Bar.f >>> b.f how can I set Bar.f as -- http://mail.python.org/mailman/listinfo/python-list
question about the id()
HI ALL: Can someone explain why the id() return the same value, and why these values are changing? Thanks you. Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> class A(object): ... def f(): ... pass ... def g(): ... pass ... >>> >>> a=A() >>> id(a.f) 11365872 >>> id(a.g) 11365872 >>> >>> >>> class B(object): ... def f(): ... print 1 ... def g(): ... print 3 ... >>> b=B() >>> id(b.f) 11365872 >>> id(b.g) 11365872 >>> id(a.f), id(a.g), id(b.f), id(b.g) (11365872, 11365872, 11365872, 11365872) >>> a.f is a.g False >>> id(a.f), id(a.g), id(b.f), id(b.g) (11492408, 11492408, 11492408, 11492408) >>> a.f is a.g False >>> id(a.f), id(a.g), id(b.f), id(b.g) (11365872, 11365872, 11365872, 11365872) >>> -- http://mail.python.org/mailman/listinfo/python-list
RE: question about the id()
HI Skip: I want to check is there any change in the instance 's methods. >>> a=A() >>> a2=A() >>> a.f == a2.f False >>> a.f is a2.f False >>> a.f is a.f False >>> If the instance methods are create on-the-fly, how to do that? Thanks. Kyo > -Original Message- > From: Skip Montanaro [mailto:[EMAIL PROTECTED] > Sent: Monday, May 16, 2005 11:09 AM > To: kyo guan > Cc: python-list@python.org > Subject: Re: question about the id() > > > kyo> Can someone explain why the id() return the same > value, and why > kyo> these values are changing? > > Instance methods are created on-the-fly. In your example the > memory associated with the a.f bound method (not the same as > the unbound method > A.f) is freed before you reference a.g. That chunk of memory > just happens to get reused for the bound method associated > with a.g. Here's a > demonstration: > > % python > Python 2.5a0 (#77, May 14 2005, 14:47:06) > [GCC 3.3 20030304 (Apple Computer, Inc. build 1671)] on darwin > Type "help", "copyright", "credits" or "license" for more > information. > >>> class A(object): > ... def f(): pass > ... def g(): pass > ... > >>> a = A() > >>> x = a.f > >>> y = a.g > >>> id(x) > 17969240 > >>> id(y) > 17969440 > >>> id(a.f) > 17969400 > >>> id(a.g) > 17969400 > > Skip -- http://mail.python.org/mailman/listinfo/python-list
profile python in embed system
HI : I want to test my system's performance. My system has a python embed. How can I test the proformance like the python module "profile" or "hotshot" . I can't use the module "profile" because my system are base callback, so I can't run my system like this: profile.run(" "). The system call the python like this : " mypython.on_xxx_callback ". but there are too many callback. and I want to get the total profile of the hope python script. How can I do? Thank you :) -- http://mail.python.org/mailman/listinfo/python-list
why python on debian without the module profile?
Hi All: Python 2.4.1 (#2, May 5 2005, 11:32:06) [GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import hotshot,hotshot.stats Traceback (most recent call last): File "", line 1, in ? File "/usr/lib/python2.4/hotshot/stats.py", line 3, in ? import profile ImportError: No module named profile >>> Python 2.3.5 (#2, May 4 2005, 08:51:39) [GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import hotshot,hotshot.stats Traceback (most recent call last): File "", line 1, in ? File "/usr/lib/python2.3/hotshot/stats.py", line 3, in ? import profile ImportError: No module named profile >>> -- http://mail.python.org/mailman/listinfo/python-list
why psyco using more memery in liunx?
Hi all: When you import psyco in python2.5, you can see the memery grow up near 40MB in linux. but the same version python and psyco, is only grow up 1MB under windows. kyo -- http://mail.python.org/mailman/listinfo/python-list
why psyco using more memery in liunx?
Hi all: When you import psyco in python2.5, you can see the memery grow up near 40MB in linux. but the same version python and psyco, is only grow up 1MB under windows. kyo -- http://mail.python.org/mailman/listinfo/python-list
a question about decorator
Hi: please look at this code. def A(): print 'warp in A' def why(self, *arg, **kw): print 'in A' print self print arg print kw #self(*arg, **kw) return why class T(object): @A() def test(g, out): print 'in test', out it will out put: warp in A in A () {} the function why will be called, why? there is no code to call it. Kyo. -- http://mail.python.org/mailman/listinfo/python-list
may be a bug in string.rstrip
Hi : Please look at this code: >>> 'exe.torrent'.rstrip('.torrent') 'ex'<- it should be 'exe', why? but this is a right answer: >>> '120.exe'.rstrip('.exe') '120' <-- this is a right value. there is a bug in the rstrip, lstrip there isn't this problem. Kyo. -- http://mail.python.org/mailman/listinfo/python-list
how to free the big list memory
Python version 2.4.3 >>> l=range(50*1024*100) after this code, you can see the python nearly using about 80MB. then I do this >>> del l after this, the python still using more then 60MB, Why the python don't free my memory? Is there any way to force the python free my memory? Thanks. Kyo. -- http://mail.python.org/mailman/listinfo/python-list
how not use memmove when insert a object in the list
Hi : python list object like a stl vector, if insert a object in the front or the middle of it, all the object after the insert point need to move backward. look at this code ( in python 2.4.3) static int ins1(PyListObject *self, int where, PyObject *v) { int i, n = self->ob_size; PyObject **items; if (v == NULL) { PyErr_BadInternalCall(); return -1; } if (n == INT_MAX) { PyErr_SetString(PyExc_OverflowError, "cannot add more objects to list"); return -1; } if (list_resize(self, n+1) == -1) return -1; if (where < 0) { where += n; if (where < 0) where = 0; } if (where > n) where = n; items = self->ob_item; for (i = n; --i >= where; ) /// here, why not use memmove? it would be more speed then this loop. items[i+1] = items[i]; Py_INCREF(v); items[where] = v; return 0; } Kyo. -- http://mail.python.org/mailman/listinfo/python-list
about the implement of the PyString_InternFromString
Hi : Hi guys: I have a question about the this API. PyObject * PyString_InternFromString(const char *cp) { PyObject *s = PyString_FromString(cp); if (s == NULL) return NULL; PyString_InternInPlace(&s); return s; } Why it always try to call PyString_FromString first? if char* cp is already in the interned dict, this PyString_FromString call is waster. so I think this API should implement as: 1. check the interned dict 2. if cp is not in the dict, then call PyString_FromString, and insert the new string in the dict 3. else : call Py_INCREF and return. Is this right? Kyo. -- http://mail.python.org/mailman/listinfo/python-list
about the implement of the PyString_InternFromString
Hi guys: I have a question about the this API. PyObject * PyString_InternFromString(const char *cp) { PyObject *s = PyString_FromString(cp); if (s == NULL) return NULL; PyString_InternInPlace(&s); return s; } Why it always try to call PyString_FromString first? if char* cp is already in the interned dict, this PyString_FromString call is waster. so I think this API should implement as: 1. check the interned dict 2. if cp is not in the dict, then call PyString_FromString, and insert the new string in the dict 3. else : call Py_INCREF and return. Is this right? Kyo. -- http://mail.python.org/mailman/listinfo/python-list