Movatterモバイル変換


[0]ホーム

URL:


[Python-Dev] Should we do away with unbound methods in Py3k?

Guido van Rossumguido at python.org
Thu Nov 22 00:18:47 CET 2007


I'm asking a Py3k question on python-dev because I'd like to haveopinions from people who haven't thought about Py3k much yet. Considerthe following example:  class C:      def foo(self): pass  C.foo(42)This currently fails with this error message:  TypeError: unbound method foo() must be called with C instance asfirst argument (got int instance instead)This message is called when isinstance(self, C) returns False, whereself is the first argument passed to the unbound method.That's nice, but there is a cost associated with this: the expression"C.foo" can't just return the function object "foo", it has to wrap itin an unbound method object. In Py3k the cost of calling an unboundmethod object goes up, because the isinstance() check may beoverloaded. This typically happens when the class C uses the specialmetaclass (abc.ABCMeta) used for virtual inheritance (see PEP 3119).in Py3k the I/O stream classes are perhaps the most common use case.Given that the error is of limited value and that otherwise theunbound method behaves exactly the same as the original functionobject, I'd like to see if there are strenuous objections againstdropping unbound method objects altogether (or at least not using themin this case), so that explicit super calls (via the unbound method)may go a little faster. Also, it would make it easier to fix thisissue:http://bugs.python.org/issue1109To illustrate the cost of the isinstance() overloading, step throughthis simple program with Py3k:import abc, pdbclass Base(metaclass=abc.ABCMeta):  @abc.abstractmethod  def foo(self):    passclass C(Base):  def foo(self):    Base.foo(self)c = C()pdb.run("c.foo()")-- --Guido van Rossum (home page:http://www.python.org/~guido/)


More information about the Python-Devmailing list

[8]ページ先頭

©2009-2026 Movatter.jp