importing a method

Martin Miller ggrp1.20.martineau at dfgh.net
Thu Dec 1 01:19:06 CET 2005


Sorry, I seldom look at the built-in __doc__ strings or use the
'help()' function.  Instead I usually refer to the html or winhelp
versions of the documentation, and for Python 2.4.1 there's nothing in
section 3.28 on the 'new' module that mentions that it deprecated -- so
thanks to you and Flávio for the information.

Using help on MethodType gave me the following somewhat surprising
output [truncated here]:
>>> import types
>>> help(types.MethodType)
Help on class instancemethod in module __builtin__:

class instancemethod(object)
 |  instancemethod(function, instance, class)
 |
 |  Create an instance method object.
[snip]

Which I take to mean that 'instancemethod' is no longer [just] a
function in the deprecated 'new' module but is also a built-in class.

However and somewhat confusingly (to me anyway), a search in the help
docs file turns up the following:

> 7.5.3  Method Objects
> There are some useful functions that are useful for working with method objects.
>
> PyTypeObject  PyMethod_Type
>   This instance of PyTypeObject represents the Python method type. This is
>   exposed to Python programs as types.MethodType.
>   ...
[snip]

Which, as you can see, claims that types.MethodType is actually an
instance of a PyTypeObject (not the class instancemethod that
help(types.MethodType) indicated).

Best,
-Martin

====
Steven D'Aprano wrote:
> On Mon, 28 Nov 2005 08:16:12 -0800, Martin Miller wrote:
>
> > First of all,why do you think the new module is deprecated?  (I can't
> > find anything in the docs to indicate this.)
>
> Did you try help(new) from an interactive prompt?
>
>
> py> new.__doc__.splitlines()[0]
> 'Create new objects of various types.  Deprecated.'
>
>
> --
> Steven.


Flávio wrote:
> > First of all,why do you think the new module is deprecated?  (I can't
> > find anything in the docs to indicate this.)
>
> Its in the docs of python 2.4. I dont know about older versions:
>
> Help on module new:
>
> NAME
>     new - Create new objects of various types.  Deprecated.
>
> FILE
>     /usr/lib/python2.4/new.py
>
> MODULE DOCS
>     /usr/share/doc/python-docs-2.4.2/html/module-new.html
>
> DESCRIPTION
>     This module is no longer required except for backward
> compatibility.
>     Objects of most types can now be created by calling the type
> object.
>
> > As for using MethodType in the types module:  There's nothing in the
> > module documentation that suggests that you can call MethodType as a
> > function as you suggest, only that it is the name of the type of
> > methods of user-defined class instances..  So, while calling it might
> > work, it sounds like you are using an undocumented feature...
>
> If you look at new.py, all it does is import the functions from types
> and rename them. For MethodType is goes like this
>
> from types import MethodType as instancemethod
>
> so instance method *is* Methodtype.
>
> Moreover, I tried and it works ;-)
>
> So this solution is perfect once adapted not to depend on "new".
> 
> Thanks,
> 
> Flávio




More information about the Python-list mailing list