Re: importing a method

2005-11-30 Thread Alex Martelli
Martin Miller <[EMAIL PROTECTED]> wrote: > You're not missing anything -- it's my own [mis-]understanding that > descriptors would only work with new-style classes, not the old-style > ones used in the OP's example. > > However your example certainly proves that is not the case, even if you > go

Re: importing a method

2005-11-30 Thread Martin Miller
You're not missing anything -- it's my own [mis-]understanding that descriptors would only work with new-style classes, not the old-style ones used in the OP's example. However your example certainly proves that is not the case, even if you go one step further and call the bound method/function: >

Re: importing a method

2005-11-30 Thread Martin Miller
Sorry, I seldom look at the built-in __doc__ strings or use the 'help()' function. Instead I usually refer to the html or winhelp versions of the documentation, and for Python 2.4.1 there's nothing in section 3.28 on the 'new' module that mentions that it deprecated -- so thanks to you and Flávio

Re: importing a method

2005-11-28 Thread Alex Martelli
Flavio <[EMAIL PROTECTED]> wrote: > > If you have a function f and want to make an instancemethod out of it, > > you can simply call f.__get__(theinstance, theclass) and that will build > > and return the new instancemethod you require. > > I think that > > f.show = MethodType(show,f) > > is le

Re: importing a method

2005-11-28 Thread Alex Martelli
Martin Miller <[EMAIL PROTECTED]> wrote: > I'd like to point out to the OP that using a function's __get__ method > this way only works with new-style classes and their instances...not > with the example in the shown in original post. Uh, why not? >>> class old: pass ... >>> def f(self): print

Re: importing a method

2005-11-28 Thread Steven D'Aprano
On Mon, 28 Nov 2005 08:16:12 -0800, Martin Miller wrote: > First of all,why do you think the new module is deprecated? (I can't > find anything in the docs to indicate this.) Did you try help(new) from an interactive prompt? py> new.__doc__.splitlines()[0] 'Create new objects of various types.

Re: importing a method

2005-11-28 Thread Flavio
> If you have a function f and want to make an instancemethod out of it, > you can simply call f.__get__(theinstance, theclass) and that will build > and return the new instancemethod you require. I think that f.show = MethodType(show,f) is less cryptic than f.__get__(instance, class) Flávio

Re: importing a method

2005-11-28 Thread Flavio
> First of all,why do you think the new module is deprecated? (I can't > find anything in the docs to indicate this.) Its in the docs of python 2.4. I dont know about older versions: Help on module new: NAME new - Create new objects of various types. Deprecated. FILE /usr/lib/python2.

Re: importing a method

2005-11-28 Thread Martin Miller
I'd like to point out to the OP that using a function's __get__ method this way only works with new-style classes and their instances...not with the example in the shown in original post. -Martin Alex Martelli wrote: > Flavio <[EMAIL PROTECTED]> wrote: > > > This "new" module sounds pretty cool,

Re: importing a method

2005-11-28 Thread Martin Miller
First of all,why do you think the new module is deprecated? (I can't find anything in the docs to indicate this.) As for using MethodType in the types module: There's nothing in the module documentation that suggests that you can call MethodType as a function as you suggest, only that it is the

Re: importing a method

2005-11-28 Thread Flavio
Addendum to my last reply: although the New Method is deprecated, new.instancemethod (from Antoon's message) can be replaced by from types import MethodType f.show = MethodType(show,f) and every thing still works. -- http://mail.python.org/mailman/listinfo/python-list

Re: importing a method

2005-11-28 Thread Alex Martelli
Flavio <[EMAIL PROTECTED]> wrote: > This "new" module sounds pretty cool, too bad its deprecated... > > I would not want to add a dependancy to a deprecated module in my code. > But maybe I'll check the code for instancemethod within it and see what > it does. If you have a function f and want t

Re: importing a method

2005-11-28 Thread Flavio
This "new" module sounds pretty cool, too bad its deprecated... I would not want to add a dependancy to a deprecated module in my code. But maybe I'll check the code for instancemethod within it and see what it does. Flávio -- http://mail.python.org/mailman/listinfo/python-list

Re: importing a method

2005-11-28 Thread Antoon Pardon
Op 2005-11-27, Flavio schreef <[EMAIL PROTECTED]>: > hi, > > I have an object defined with a number of hardcoded methods. > > Class soandso: > def __init__(self): > self.this = 0 > self.that = 1 > def meth1(self): > ... > def meth2(self): > ... > def

Re: importing a method

2005-11-28 Thread Flavio
There only one puzzle left to solve: altough the solution I proposed works, this variant has problems: >>> class Foo: name='John' >>> a=Foo() >>> def p(): print 'Hi, %s!'%self.name >>> a.met=p >>> a.met.self = a >>>a.met() NameError: global name 'self' is not defined Thi

Re: importing a method

2005-11-28 Thread Flavio
If you read my original post, I had no intention of atributing the user's method to the class, but to the instance. Anyway I figure it out myself, and its quite a Pythonic solution: >>> class Foo: name='John' >>> a=Foo() >>> def p(parent): self=parent print 'H

Re: importing a method

2005-11-28 Thread Flavio
Because, by the time the user function is imported and attributed to the custom method, soandso has already been instantiated and contains the information tha needs to accessed by the user's function. -- http://mail.python.org/mailman/listinfo/python-list

Re: importing a method

2005-11-27 Thread Chris Curvey
why not just have your user subclass "soandso" and override the definition of "custom"? from soandso import soandso class MyClass(soandso): def custom(self): self.theother = 3 c = MyClass() -- http://mail.python.org/mailman/listinfo/python-list

Re: importing a method

2005-11-27 Thread Ben Finney
Flavio <[EMAIL PROTECTED]> wrote: > Class soandso: > def __init__(self): > self.this = 0 > self.that = 1 > def meth1(self): > ... > def meth2(self): > ... > def custom(self): > pass > > I want to allow the user to write a python module that d

importing a method

2005-11-27 Thread Flavio
hi, I have an object defined with a number of hardcoded methods. Class soandso: def __init__(self): self.this = 0 self.that = 1 def meth1(self): ... def meth2(self): ... def custom(self): pass I want to allow the user to write a python modu