On Class namespaces, calling methods

Duncan Booth duncan.booth at invalid.invalid
Sat Apr 10 18:35:29 CEST 2010


vsoler <vicente.soler at gmail.com> wrote:

> On Apr 10, 4:46 pm, Duncan Booth <duncan.bo... at invalid.invalid> wrote:
>> vsoler <vicente.so... at gmail.com> wrote:
>> > I get the following error message:
>>
>> >      TypeError: m() takes no arguments (1 given)
>>
<snip>
>> Which version of Python are you using? Python 2.6 gives:
>>
>> TypeError: unbound method m() must be called with Uno instance as first
>> argument (got nothing instead)
>>
<snip>
 
> Hello Duncan,
> 
> Your error message is correct if you use         class Uno(object):
> However, the error message is different under    class Uno:
> 

Odd. That's not what I get:

Python 2.6.1 (r26:67517, Dec  4 2008, 16:59:09) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************
    
IDLE 2.6.1      
>>> class Uno:
    a=1
    def m():
        print "mouse"

        
>>> Uno.m()

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    Uno.m()
TypeError: unbound method m() must be called with Uno instance as first argument (got nothing instead)
>>> 

Anyway, the moral is never, ever to use old-style classes in Python 2.x. 
You will get weird and unexpected results.



More information about the Python-list mailing list