Re: [Python-Dev] Let's get rid of unbound methods

2013-06-04 Thread Steven D'Aprano

On 04/06/13 12:43, 探晴 wrote:
nothing


Your email appears to be blank, except for a large chunk of HTML code. Did you 
have something specific to say other than the subject line?


As for unbound methods, Guido's time machine strikes again. They're already 
gone in Python 3.


py class X:
... def spam(self):
... pass
...
py X.spam
function spam at 0xb7bd2dac
py X().spam
bound method X.spam of __main__.X object at 0xb7bd0e0c



--
Steven
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


[Python-Dev] Let's get rid of unbound methods

2013-06-03 Thread 探晴
 

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Let's get rid of unbound methods

2005-01-28 Thread Greg Ewing
Tim Peters wrote:
I expect that's because he stopped working on Zope code, so actually
thinks it's odd again to see a gazillion methods like:
class Registerer(my_base):
def register(*args, **kws):
my_base.register(*args, **kws)
I second that! My PyGUI code is *full* of __init__
methods like that, because of my convention for supplying
initial values of properties as keyword arguments.
--
Greg
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Let's get rid of unbound methods

2005-01-04 Thread Bob Ippolito
On Jan 4, 2005, at 1:28 PM, Guido van Rossum wrote:
Let's get rid of unbound methods. When class C defines a method f, C.f
should just return the function object, not an unbound method that
behaves almost, but not quite, the same as that function object. The
extra type checking on the first argument that unbound methods are
supposed to provide is not useful in practice (I can't remember that
it ever caught a bug in my code) and sometimes you have to work around
it; it complicates function attribute access; and the overloading of
unbound and bound methods on the same object type is confusing. Also,
the type checking offered is wrong, because it checks for subclassing
rather than for duck typing.
+1
I like this idea.  It may have some effect on current versions of 
PyObjC though, because we really do care about what self is in order to 
prevent crashes.  This is not a discouragement; we are already using 
custom descriptors and a metaclass, so it won't be a problem to do this 
ourselves if we are not doing it already.  I'll try and find some time 
later in the week to play with this patch to see if it does break 
PyObjC or not.  If it breaks PyObjC, I can sure that PyObjC 1.3 will be 
compatible with such a runtime change, as we're due for a refactoring 
in that area anyway.

-bob
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Let's get rid of unbound methods

2005-01-04 Thread Jack Diederich
On Tue, Jan 04, 2005 at 10:28:03AM -0800, Guido van Rossum wrote:
 In my blog I wrote:
 
 Let's get rid of unbound methods. When class C defines a method f, C.f
 should just return the function object, not an unbound method that
 behaves almost, but not quite, the same as that function object. The
 extra type checking on the first argument that unbound methods are
 supposed to provide is not useful in practice (I can't remember that
 it ever caught a bug in my code) and sometimes you have to work around
 it; it complicates function attribute access; and the overloading of
 unbound and bound methods on the same object type is confusing. Also,
 the type checking offered is wrong, because it checks for subclassing
 rather than for duck typing.
 
 Does anyone think this is a bad idea? Anyone want to run with it?
 
I like the idea, it means I can get rid of this[1]

func = getattr(cls, 'do_command', None)
setattr(cls, 'do_command', staticmethod(func.im_func)) # don't let anyone on 
c.l.py see this

.. or at least change the comment *grin*,

-Jack

[1] 
http://cvs.sourceforge.net/viewcvs.py/lyntin/lyntin40/sandbox/leantin/mudcommands.py?view=auto
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


RE: [Python-Dev] Let's get rid of unbound methods

2005-01-04 Thread Raymond Hettinger
[Guido van Rossum]
  Let's get rid of unbound methods. 

+1



[Jim Fulton]
 duck typing?

Requiring a specific interface instead of a specific type.



[Guido]
  Does anyone think this is a bad idea?
[Jim]
 It *feels* very disruptive to me, but I'm probably wrong.
 We'll still need unbound builtin methods, so the concept won't
 go away. In fact, the change would mean that the behavior between
 builtin methods and python methods would become more inconsistent.

The type change would be disruptive and guaranteed to break some code.
Also, it would partially breakdown the distinction between functions and
methods.

The behavior, on the other hand, would remain essentially the same (sans
type checking).



Raymond

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Let's get rid of unbound methods

2005-01-04 Thread Jp Calderone
On Tue, 4 Jan 2005 10:28:03 -0800, Guido van Rossum [EMAIL PROTECTED] wrote:
In my blog I wrote:
 
 Let's get rid of unbound methods. When class C defines a method f, C.f
 should just return the function object, not an unbound method that
 behaves almost, but not quite, the same as that function object. The
 extra type checking on the first argument that unbound methods are
 supposed to provide is not useful in practice (I can't remember that
 it ever caught a bug in my code) and sometimes you have to work around
 it; it complicates function attribute access; and the overloading of
 unbound and bound methods on the same object type is confusing. Also,
 the type checking offered is wrong, because it checks for subclassing
 rather than for duck typing.
 

  This would make pickling (or any serialization mechanism) of 
`Class.method' based on name next to impossible.  Right now, with
the appropriate support, this works:

 import pickle
 class Foo:
... def bar(self): pass
... 
 pickle.loads(pickle.dumps(Foo.bar))
unbound method Foo.bar
 

  I don't see how it could if Foo.bar were just a function object.

  Jp
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Let's get rid of unbound methods

2005-01-04 Thread Jp Calderone
On Tue, 04 Jan 2005 20:02:06 GMT, Jp Calderone [EMAIL PROTECTED] wrote:
On Tue, 4 Jan 2005 10:28:03 -0800, Guido van Rossum [EMAIL PROTECTED] wrote:
 In my blog I wrote:
  
  Let's get rid of unbound methods. When class C defines a method f, C.f
  should just return the function object, not an unbound method that
  behaves almost, but not quite, the same as that function object. The
  extra type checking on the first argument that unbound methods are
  supposed to provide is not useful in practice (I can't remember that
  it ever caught a bug in my code) and sometimes you have to work around
  it; it complicates function attribute access; and the overloading of
  unbound and bound methods on the same object type is confusing. Also,
  the type checking offered is wrong, because it checks for subclassing
  rather than for duck typing.
  
 
   This would make pickling (or any serialization mechanism) of 
 `Class.method' based on name next to impossible.  Right now, with
 the appropriate support, this works:

  It occurs to me that perhaps I was not clear enough here.  

  What I mean is that it is possible to serialize unbound methods 
currently, because they refer to both their own name, the name of 
their class object, and thus indirectly to the module in which they 
are defined.

  If looking up a method on a class object instead returns a function, 
then the class is no longer knowable, and most likely the function will
not have a unique name which can be used to allow a reference to it to 
be serialized.

  In particular, I don't see how one will be able to write something 
equivalent to this:

import new, copy_reg, types

def pickleMethod(method):
return unpickleMethod, (method.im_func.__name__,
method.im_self,
method.im_class)

def unpickleMethod(im_name, im_self, im_class):
unbound = getattr(im_class, im_name)
if im_self is None:
return unbound
return new.instancemethod(unbound.im_func,
  im_self,
  im_class)

copy_reg.pickle(types.MethodType, 
pickleMethod, 
unpickleMethod)

  But perhaps I am just overlooking the obvious.

  Jp
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Let's get rid of unbound methods

2005-01-04 Thread Jp Calderone
On Tue, 4 Jan 2005 12:18:15 -0800, Guido van Rossum [EMAIL PROTECTED] wrote:
[me]
   Actually, unbound builtin methods are a different type than bound
   builtin methods:
 
 [Jim]
  Of course, but conceptually they are similar.  You would still
  encounter the concept if you got an unbound builtin method.
 
 Well, these are all just implementation details. They really are all
 just callables.
 
 [Jp]
This would make pickling (or any serialization mechanism) of
  `Class.method' based on name next to impossible.  Right now, with
  the appropriate support, this works:
  
   import pickle
   class Foo:
  ... def bar(self): pass
  ...
   pickle.loads(pickle.dumps(Foo.bar))
  unbound method Foo.bar
  
  
I don't see how it could if Foo.bar were just a function object.
 
 Is this a purely theoretical objection or are you actually aware of
 anyone doing this? Anyway, that approach is pretty limited -- how
 would you do it for static and class methods, or methods wrapped by
 other decorators?

  It's not a feature I often depend on, however I have made use of 
it on occassion.  Twisted's supports serializing unbound methods 
this way, primarily to enhance the useability of tap files (a feature 
whereby an application is configured by constructing a Python object 
graph and then pickled to a file to later be loaded and run).

  Objection may be too strong a word for my stance here, I just 
wanted to point out another potentially incompatible behavior change.
I can't think of any software which I cam currently developing or 
maintaining which benefits from this feature, it just seems 
unfortunate to further complicate the already unpleasant business 
of serialization.

  Jp
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Let's get rid of unbound methods

2005-01-04 Thread Phillip J. Eby
At 11:40 AM 1/4/05 -0800, Guido van Rossum wrote:
[Jim]
 We'll still need unbound builtin methods, so the concept won't
 go away. In fact, the change would mean that the behavior between
 builtin methods and python methods would become more inconsistent.
Actually, unbound builtin methods are a different type than bound
builtin methods:
 type(list.append)
type 'method_descriptor'
 type([].append)
type 'builtin_function_or_method'

Compare this to the same thing for a method on a user-defined class:
 type(C.foo)
type 'instancemethod'
 type(C().foo)
type 'instancemethod'
(The 'instancemethod' type knows whether it is a bound or unbound
method by checking whether im_self is set.)
[Phillip]
 Code that currently does 'aClass.aMethod.im_func' in order to access the
 function object would break, as would code that inspects 'im_self' to
 determine whether a method is a class or instance method.  (Although code
 of the latter sort would already break with static methods, I suppose.)
Right. (But I think you're using the terminology in a cunfused way --
im_self distinguishes between bould and unbound methods. Class methods
are a different beast.)
IIUC, when you do 'SomeClass.aMethod', if 'aMethod' is a classmethod, then 
you will receive a bound method with an im_self of 'SomeClass'.  So, if you 
are introspecting items listed in 'dir(SomeClass)', this will be your only 
clue that 'aMethod' is a class method.  Similarly, the fact that you get an 
unbound method object if 'aMethod' is an instance method, allows you to 
distinguish it from a static method (if the object is a function).

That is, I'm saying that code that looks at the type and attributes of 
'aMethod' as retrieved from 'SomeClass' will now not be able to distinguish 
between a static method and an instance method, because both will return a 
function instance.

However, the 'inspect' module uses __dict__ rather than getattr to get at 
least some attributes, so it doesn't rely on this property.


I guess for backwards compatibility, function objects could implement
dummy im_func and im_self attributes (im_func returning itself and
im_self returning None), while issuing a warning that this is a
deprecated feature.
+1 on this part if the proposal goes through.
On the proposal as a whole, I'm -0, as I'm not quite clear on what this is 
going to simplify enough to justify the various semantic impacts such as 
upcalls, pickling, etc.  Method objects will still have to exist, so ISTM 
that this is only going to streamline the __get__(None,type) branch of 
functions' descriptor code, and the check for im_self is None in the 
__call__ of method objects.  (And maybe some eval loop shortcuts for 
calling methods?)

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Let's get rid of unbound methods

2005-01-04 Thread Josiah Carlson

Tim Peters [EMAIL PROTECTED] wrote:
 
 [Tim Peters]
  ...  Unbound methods are used most often (IME) to call a
  base-class method from a subclass, like
  my_base.the_method(self, ...).
   It's especially easy to forget to write `self, ` there, and the
  exception msg then is quite focused because of that extra bit of
  type checking.  Otherwise I expect we'd see a more-mysterious
  AttributeError or TypeError when the base method got around to
  trying to do something with the bogus `self` passed to it.
 
 [Josiah Carlson]
  Agreed.
 
 Well, it's not that easy to agree with.  Guido replied that most such
 cases would raise an argument-count-mismatch exception instead.  I
 expect that's because he stopped working on Zope code, so actually
 thinks it's odd again to see a gazillion methods like:
 
 class Registerer(my_base):
 def register(*args, **kws):
 my_base.register(*args, **kws)
 
 I bet he even presumes that if you chase such chains long enough,
 you'll eventually find a register() method *somewhere* that actually
 uses its arguments wink.

If type checking is important, one can always add it using decorators. 
Then again, I would be willing to wager that most people wouldn't add it
due to laziness, until it bites them for more than a few hours worth of
debugging time.


  While it seems that super() is the 'modern pradigm' for this,
  I have been using base.method(self, ...) for years now, and have
  been quite happy with it.  After attempting to convert my code to
  use the super() paradigm, and having difficulty, I discovered James
  Knight's Python's Super Considered Harmful (available at
  http://www.ai.mit.edu/people/jknight/super-harmful/ ), wherein I
  discovered how super really worked (I should have read the
  documention in the first place), and reverted my changes to the
  base.method version.
 
 How did super() get into this discussion?  I don't think I've ever
 used it myself, but I avoid fancy inheritance graphs in my own code,
 so can live with anything.

It was my misunderstanding of your statement in regards to base.method. 
I had thought that base.method(self, ...) would stop working, and
attempted to discover how one would be able to get the equivalent back,
regardless of the inheritance graph.


  I could live with it too, but I would probably use an equivalent of the
  following (with actual type checking):
 
  def mysuper(typ, obj):
 lm = list(o.__class__.__mro__)
 indx = lm.index(typ)
 if indx == 0:
 return obj
 return super(lm[indx-1], obj)
 
  All in all, I'm -0.  I don't desire to replace all of my base.method
  with mysuper(base, obj).method, but if I must sacrifice
  convenience for the sake of making Python 2.5's implementation
  simpler, I guess I'll deal with it. My familiarity with grep's regular
  expressions leaves something to be desired, so I don't know how
  often base.method(self,...) is or is not used in the standard library.
 
 I think there may be a misunderstanding here.  Guido isn't proposing
 that base.method(self, ...) would stop working -- it would still work
 fine.  The result of base.method would still be a callable object:  it
 would no longer be of an unbound method type (it would just be a
 function), and wouldn't do special checking on the first argument
 passed to it anymore, but base.method(self, ...) would still invoke
 the base class method.  You wouldn't need to rewrite anything (unless
 you're doing heavy-magic introspection, picking callables apart).

Indeed, there was a misunderstanding on my part.  I misunderstood your
discussion of base.method(self, ...) to mean that such things would stop
working.  My apologies.


 - Josiah

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com