From: Jonathan Gardner on
On Tue, Mar 16, 2010 at 2:04 AM, Bruno Desthuilliers
<bruno.42.desthuilliers(a)websiteburo.invalid> wrote:
> lallous a écrit :
>>
>> What is the difference between the reference in 'F' and 'func_tbl' ?
>
> Answer here:
>
> http://wiki.python.org/moin/FromFunctionToMethod
>

Among all the things in the Python language proper, this is probably
the most confusing thing for new programmers. Heck, even experienced
programmers get tangled up because they project how they think things
should work on to the Python model.

The second most confusing thing is probably how objects get instantiated.

--
Jonathan Gardner
jgardner(a)jonathangardner.net
From: Lie Ryan on
On 03/17/2010 05:59 AM, Jason Tackaberry wrote:
> On Tue, 2010-03-16 at 10:04 +0100, Bruno Desthuilliers wrote:
>> Answer here:
>>
>> http://wiki.python.org/moin/FromFunctionToMethod
>
> I have a sense I used to know this once upon a time, but the question
> came to my mind (possibly again) and I couldn't think of an answer:
>
> Why not create the bound methods at instantiation time, rather than
> using the descriptor protocol which has the overhead of creating a new
> bound method each time the method attribute is accessed?

Because people wanted it like so. There was once, a time when python
doesn't have the descriptor protocol (old-style classes) and many people
feels that a high-level language like python should provide some
additional hooks for customizing attribute access which the existing
solutions like __getattr__ and __setattr__ couldn't easily provide. The
result is new-style class. Most people probably would never need to use
descriptor protocol directly, since the immediate benefit of descriptor
protocol are property(), classmethod(), and instancemethod() decorators
which, without descriptor protocol, would never become a possibility.
From: Steven D'Aprano on
On Wed, 17 Mar 2010 15:57:17 +1100, Lie Ryan wrote:

> Most people probably would never need to use
> descriptor protocol directly, since the immediate benefit of descriptor
> protocol are property(), classmethod(), and instancemethod() decorators
> which, without descriptor protocol, would never become a possibility.


There's an instancemethod decorator? Where?

Are you thinking of staticmethod? "instancemethod", if you mean what I
think you mean, doesn't need a decorator because it is the default
behaviour for new-style classes.


--
Steven
From: Patrick Maupin on
On Mar 16, 1:59 pm, Jason Tackaberry <t...(a)urandom.ca> wrote:
> Why not create the bound methods at instantiation time, rather than
> using the descriptor protocol which has the overhead of creating a new
> bound method each time the method attribute is accessed?

Well, for one thing, Python classes are open. They can be added to at
any time. For another thing, you might not ever use most of the
methods of an instance, so it would be a huge waste to create those.

Also, this area has been optimized for normal usage patterns quite
heavily, to the point where attempted "optimizations" can lead to
results that are, on the surface, quite counterintuitive.

For example, if you want to take the length of a lot of different
strings, you might think you could save time by binding a local
variable to str.__len__ and using that on the strings. Here is an
example:

>>> def a(s, count, lenfunc):
.... for i in xrange(count):
.... z = lenfunc(s)
....
>>> a('abcdef', 100000000, len)
>>> a('abcdef', 100000000, str.__len__)

Running cPython 2.6 on my machine, len() runs about 3 times faster
than str.__len__(). The overhead of checking that an object is usable
with a particular class method far outweighs the cost of creating the
bound method!

So, one thought for the OP. Whenever I have a dictionary that
contains class methods in it, if I'm going to use it heavily, I often
recode it to create the dictionary at object creation time with bound
methods in it.

Regards,
Pat
From: Bruno Desthuilliers on
Patrick Maupin a �crit :
> On Mar 16, 1:59 pm, Jason Tackaberry <t...(a)urandom.ca> wrote:
>> Why not create the bound methods at instantiation time, rather than
>> using the descriptor protocol which has the overhead of creating a new
>> bound method each time the method attribute is accessed?
>
> Well, for one thing, Python classes are open. They can be added to at
> any time. For another thing, you might not ever use most of the
> methods of an instance, so it would be a huge waste to create those.

A possible optimization would be a simple memoization on first access.