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.
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to