what should we use instead of the 'new' module?

Discussion in 'Python' started by Flavio, Nov 13, 2008.

  1. Flavio

    Flavio Guest

    How is this code going to look like in Python 3.0? (it's deprecated
    according to http://docs.python.org/library/new.html#module-new, but
    it does not tell what to use instead)

    method = new.instancemethod(raw_func, None, cls)
    setattr(cls, name, method)

    Can we write code in python2.5/2.6 that will work in 3.0?
    Flavio, Nov 13, 2008
    #1
    1. Advertising

  2. Flavio

    Robert Kern Guest

    Flavio wrote:
    > How is this code going to look like in Python 3.0? (it's deprecated
    > according to http://docs.python.org/library/new.html#module-new, but
    > it does not tell what to use instead)
    >
    > method = new.instancemethod(raw_func, None, cls)
    > setattr(cls, name, method)


    Use the type objects in the types module.

    In [8]: import types

    In [9]: class A(object):
    ...: pass
    ...:

    In [10]: def foo(self, x):
    ....: print x
    ....:
    ....:

    In [11]: A.foo = types.MethodType(foo, None, A)

    In [12]: a = A()

    In [13]: a.foo('See?')
    See?

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
    Robert Kern, Nov 13, 2008
    #2
    1. Advertising

  3. Flavio

    Guest

    Robert,

    Appreciate your response.

    However Guido says here that types was never intended to be used like
    that:

    http://bugs.python.org/msg58023

    quote: "The types module was only ever intended for type
    checking, not for creating new instances.

    The correct solution will be to use whatever we end up deciding about
    pyvm. Certainly the types module will go."

    So that does not seem like a very long term solution to me?

    best,

    Flavio

    On Nov 12, 9:39 pm, Robert Kern <> wrote:
    > Flavio wrote:
    > > How is this code going to look like in Python 3.0? (it's deprecated
    > > according tohttp://docs.python.org/library/new.html#module-new, but
    > > it does not tell what to use instead)

    >
    > >  method = new.instancemethod(raw_func, None, cls)
    > >  setattr(cls, name, method)

    >
    > Use the type objects in the types module.
    >
    > In [8]: import types
    >
    > In [9]: class A(object):
    >     ...:     pass
    >     ...:
    >
    > In [10]: def foo(self, x):
    >     ....:     print x
    >     ....:
    >     ....:
    >
    > In [11]: A.foo = types.MethodType(foo, None, A)
    >
    > In [12]: a = A()
    >
    > In [13]: a.foo('See?')
    > See?
    >
    > --
    > Robert Kern
    >
    > "I have come to believe that the whole world is an enigma, a harmless enigma
    >   that is made terrible by our own mad attempt to interpret it as though it had
    >   an underlying truth."
    >    -- Umberto Eco
    , Nov 13, 2008
    #3
  4. Flavio

    Benjamin Guest

    On Nov 12, 9:54 pm, wrote:
    > Robert,
    >
    > Appreciate your response.
    >
    > However Guido says here that types was never intended to be used like
    > that:
    >
    > http://bugs.python.org/msg58023
    >
    > quote: "The types module was only ever intended for type
    > checking, not for creating new instances.
    >
    > The correct solution will be to use whatever we end up deciding about
    > pyvm. Certainly the types module will go."
    >
    > So that does not seem like a very long term solution to me?


    Despite what Guido says, the types module will probably still be with
    us for years to come, so I wouldn't worry about it.
    Benjamin, Nov 13, 2008
    #4
  5. Flavio

    Robert Kern Guest

    wrote:
    > Robert,
    >
    > Appreciate your response.
    >
    > However Guido says here that types was never intended to be used like
    > that:
    >
    > http://bugs.python.org/msg58023
    >
    > quote: "The types module was only ever intended for type
    > checking, not for creating new instances.
    >
    > The correct solution will be to use whatever we end up deciding about
    > pyvm. Certainly the types module will go."
    >
    > So that does not seem like a very long term solution to me?


    Hmm. Interesting. Anyways, if adding a method to a class is the only use case
    you care about, you can just add the function itself. You don't have to make a
    method object from it.

    In [14]: A.foo = foo

    In [16]: a = A()

    In [17]: a.foo('See?')
    See?

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
    Robert Kern, Nov 13, 2008
    #5
  6. Flavio

    John Roth Guest

    On Nov 12, 8:32 pm, Flavio <> wrote:
    > How is this code going to look like in Python 3.0? (it's deprecated
    > according tohttp://docs.python.org/library/new.html#module-new, but
    > it does not tell what to use instead)
    >
    >  method = new.instancemethod(raw_func, None, cls)
    >  setattr(cls, name, method)
    >
    > Can we write code in python2.5/2.6 that will work in 3.0?


    I'm not sure why your example works: instancemethod is
    used internally to wrap the function object when it's
    invoked. The only other use for it
    is to add a method to an instance, not
    to a class. As another poster said, an instance method
    in a class is simply the function object. Static and
    class methods require wrappers, but those are both
    built-in functions and they also have decorators.

    John Roth
    John Roth, Nov 13, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    6,364
    Greg Comeau
    Oct 19, 2005
  2. dolphin
    Replies:
    4
    Views:
    312
    Jorgen Grahn
    Aug 25, 2007
  3. mike3
    Replies:
    14
    Views:
    602
  4. Mike Kent
    Replies:
    7
    Views:
    269
    Eric Brunel
    Feb 4, 2008
  5. Replies:
    1
    Views:
    225
Loading...

Share This Page