[New-bugs-announce] [issue20309] Class method descriptors are different between builtin and user classes

Larry Hastings report at bugs.python.org
Mon Jan 20 07:43:38 CET 2014

New submission from Larry Hastings:

I found something curious while experimenting with types tonight.

Everybody knows, if you access a descriptor in a class through the normal attribute-getting methods, you actually get the result of calling its '__get__' method.  If you want to get your hands on the descriptor itself, you can be sneaky and pull it out through the class's __dict__.

I did an experiment tonight, accessing a bunch of methods in a bunch of different ways.

The methods I tried were
  * an instance method,
  * a class method, and
  * a static method.

I accessed them
  * as an instance attribute,
  * as a class attribute, and
  * through the class __dict__.

And for each of the above, I tried
  * one user method and
  * one built-in method.

That's a total of eighteen methods tried (3x3x2).

For each one, I printed out whether or not it was callable.  I found the results startling: fifteen were, but three (!) were not.

All three that weren't were accessed through the class dict.  They were:
  * Class method, from builtin class ( dict.__dict__['fromkeys'] )
  * Static method, from user class
  * Static method, from builtin class ( str.__dict__['maketrans'] )

I find it strange that the static method descriptors aren't callable.
I find it *even stranger* that *one* of the class method descriptors isn't callable, but the other one is.

I guess a foolish consistency is the hobgoblin of my small mind, but... shouldn't all eighteen of these objects be callable?

Attached is the test harness I wrote.

(p.s. I attached you guys as nosy because I thought you might be interested.)

components: Interpreter Core
files: curious.py
messages: 208524
nosy: arigo, larry, ncoghlan, pitrou
priority: low
severity: normal
stage: needs patch
status: open
title: Class method descriptors are different between builtin and user classes
type: behavior
versions: Python 3.4
Added file: http://bugs.python.org/file33559/curious.py

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list