[Python-Dev] Are PyCFunctions supposed to invisibly consume self when used as a method? (original) (raw)

Brett Cannon brett at python.org
Sat Jun 12 02:35:22 CEST 2010


The logging module taught me something today about the difference of a function defined in C and a function defined in Python::

import importlib

class Base: def imp(self, name): return self.import_(name)

class CVersion(Base): import_ = import

class PyVersion(Base): import_ = importlib.import

CFunction().imp('tokenize') PyFunction().imp('tokenize') # Fails!

Turns out the use of import works while the importlib version fails. Why does importlib fail? Because the first argument to the importlib.import function is an instance of PyVersion, not a string. And yet the import version works as if the self argument is never passed to it!

This "magical" ignoring of self seems to extend to any PyCFunction. Is this dichotomy intentional or just a "fluke"? Maybe this is a hold-over from before we had descriptors and staticmethod, but now that we have these things perhaps this difference should go away.



More information about the Python-Dev mailing list