Terry Reedy wrote:
> "Ron Adam" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
>
>>Actually I think I'm getting more confused. At some point the function
>>is wrapped. Is it when it's assigned, referenced, or called?
>
>
> When it is referenced via the class.
Ok, that's what
Terry Reedy wrote:
> "Ron Adam" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
>
>>Actually I think I'm getting more confused. At some point the function
>>is wrapped. Is it when it's assigned, referenced, or called?
>
>
> When it is referenced via the class.
> If you lookup i
"Ron Adam" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Actually I think I'm getting more confused. At some point the function
> is wrapped. Is it when it's assigned, referenced, or called?
When it is referenced via the class.
If you lookup in class.__dict__, the function is
Steven D'Aprano wrote:
> On Tue, 27 Sep 2005 16:42:21 +, Ron Adam wrote:
>
>
>>>def beacon(self, x):
...print "beacon + %s" % x
...
>>>
>>>
>>>Did you mean bacon? *wink*
>>
>>Of course... remembering arbitrary word letter sequences is probably my
>>worst skill. ;-) Tha
On Tue, 27 Sep 2005 16:42:21 +, Ron Adam wrote:
>>> >>> def beacon(self, x):
>>>...print "beacon + %s" % x
>>>...
>>
>>
>> Did you mean bacon? *wink*
>
> Of course... remembering arbitrary word letter sequences is probably my
> worst skill. ;-) That, and I think for some reason the na
Steven D'Aprano wrote:
> On Sun, 25 Sep 2005 14:52:56 +, Ron Adam wrote:
>
>
>>Steven D'Aprano wrote:
>>
>>
>>
>>>Or you could put the method in the class and have all instances recognise
>>>it:
>>>
>>>py> C.eggs = new.instancemethod(eggs, None, C)
>>>py> C().eggs(3)
>>>eggs * 3
>>
>>Why not
Steven D'Aprano wrote:
> py> class Klass:
> ... pass
> ...
> py> def eggs(self, x):
> ... print "eggs * %s" % x
> ...
> py> inst = Klass() # Create a class instance.
> py> inst.eggs = eggs # Dynamically add a function/method.
> py> inst.eggs(1)
> Traceback (most recent call last):
> Fil
On Sun, 25 Sep 2005 14:52:56 +, Ron Adam wrote:
> Steven D'Aprano wrote:
>
>
>> Or you could put the method in the class and have all instances recognise
>> it:
>>
>> py> C.eggs = new.instancemethod(eggs, None, C)
>> py> C().eggs(3)
>> eggs * 3
>
> Why not just add it to the class directly
Steven D'Aprano wrote:
> Or you could put the method in the class and have all instances recognise
> it:
>
> py> C.eggs = new.instancemethod(eggs, None, C)
> py> C().eggs(3)
> eggs * 3
Why not just add it to the class directly? You just have to be sure
it's a class and not an instance of a cl
One caveat, as I recently discovered, to dynamically adding methods is
that it doesn't work for __foo__ methods. For example, you can't make
an object into an iterator by dynamically assigning bound methods to
obj.__iter__ and obj.next. Same thing with __getitem__, __setitem__,
etc; adding them dir
Suppose I create a class with some methods:
py> class C:
... def spam(self, x):
... print "spam " * x
... def ham(self, x):
... print "ham * %s" % x
...
py> C().spam(3)
spam spam spam
>>> C().ham(3)
ham * 3
To dynamically remove the methods, delete them from the cl
11 matches
Mail list logo