Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simple Traits Questions

5 views
Skip to first unread message

Ethan Furman

unread,
Mar 27, 2010, 9:19:02 AM3/27/10
to Python
Okay, different post for my actual questions. :)

On the PyPI page for strait (http://pypi.python.org/pypi/strait/0.5.1)
it has the example of choosing which methods to keep in the composed class:

class TOSWidget(BaseWidget):
__metaclass__ = include(Pack, Place, Grid)
info = Pack.info.im_func
config = Pack.config.im_func
configure = Pack.configure.im_func
slaves = Pack.slaves.im_func
forget = Pack.forget.im_func
propagate = Pack.propagate.im_func

My question is:

Why use

info = Pack.info.im_func

instead of

info = Pack.info

?

Any and all enlightenment appreciated!

~Ethan~

Robert Kern

unread,
Mar 27, 2010, 8:23:51 PM3/27/10
to pytho...@python.org

Pack.info is an unbound method object attached to the Pack class, not a function
object. It has some extra semantics on top of functions and is tied to the Pack
class. The .im_func attribute gets the actual function object underneath. When
defining the TOSWidget class, the objects defined in the suite under the class:
statement need to be actual functions in order to be turned into unbound methods
attached to the TOSWidget class.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Ethan Furman

unread,
Mar 28, 2010, 9:46:36 AM3/28/10
to pytho...@python.org

Ah, thank you.

In Python 3, though, would the Pack.info form be correct?

~Ethan~

Robert Kern

unread,
Mar 28, 2010, 3:03:49 PM3/28/10
to pytho...@python.org

Seems so:

Python 3.1 (r31:73578, Jun 27 2009, 21:49:46)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class Pack():
... def info(self, x):
... pass
...
>>> Pack.info
<function info at 0xe8390>

Terry Reedy

unread,
Mar 28, 2010, 6:06:21 PM3/28/10
to pytho...@python.org

Yes. One of the simplifications of Py3.

0 new messages