new style class

G

gert

class Test(object):

def execute(self,v):
return v

def escape(v):
return v

if __name__ == '__main__':
gert = Test()
print gert.m1('1')
print Test.m2('2')

Why doesn't this new style class work in python 2.5.1 ?
 
B

Boris Borcic

gert said:
class Test(object):

def execute(self,v):
return v

def escape(v):
return v

if __name__ == '__main__':
gert = Test()
print gert.m1('1')
print Test.m2('2')

Why doesn't this new style class work in python 2.5.1 ?

why should it ?
 
G

gert

I don't know I thought it was supported from 2.2?

oops the code is like this but doesn't work

class Test(object):

def m1(self,v):
return v

def m2(v):
return v

if __name__ == '__main__':
gert = Test()
print gert.m1('1')
print Test.m2('2')
 
W

Wildemar Wildenburger

gert said:
I don't know I thought it was supported from 2.2?
Look at the error you get.

Repeat: LOOK AT THE ERROR YOU GET!

(Hint: "m1" not in ("execute", "escape"))

Nothing to do with old- or new-style classes.
/W
 
T

Tim Chase

gert said:
I don't know I thought it was supported from 2.2?

I don't recall Python supporting non-existent method-calls (such
as m1/m2 when they aren't actually defined) in ANY version of Python.

But once you change that, you'll likely also want to investigate
the classmethod decorator if you want to create class-methods.

-tkc
 
N

Nigel Rantor

gert said:
I don't know I thought it was supported from 2.2?

I think what Boris was being exceedingly unhelpful in saying was "why
should it work when you're calling methods that do not exist"

I don't see 'm1' or 'm2' defined for the class 'Test'.

n
 
M

Martin Sand Christensen

gert> Why doesn't this new style class work in python 2.5.1 ?

Whether you declare your class as a new style class or an old style
class, your code is completely and utterly broken. Calling non-existing
methods has never been a good way of getting things done. :)

Martin
 
W

Wildemar Wildenburger

gert said:
oops the code is like this but doesn't work

class Test(object):

def m1(self,v):
return v

def m2(v):
return v

if __name__ == '__main__':
gert = Test()
print gert.m1('1')
print Test.m2('2')


Well, what do you think:
In [9]: gert = Test()

In [10]: print gert.m1('1')
....: print Test.m2('2')
....:
1
---------------------------------------------------------------------------
<type 'exceptions.TypeError'> Traceback (most recent call last)

/home/wildemar/<ipython console> in <module>()

<type 'exceptions.TypeError'>: unbound method m2() must be called with Test instance as first argument (got str instance instead)

(Another hint: look at what m1 has that m2 lacks.)

/W
 
B

Boris Borcic

gert wrote:
[...]
oops the code is like this but doesn't work

class Test(object):

def m1(self,v):
return v

def m2(v):
return v

if __name__ == '__main__':
gert = Test()
print gert.m1('1')
print Test.m2('2')

You should put a '@staticmethod' decorator before your m2 method definition
 
B

Bjoern Schliessmann

gert said:
oops the code is like this but doesn't work

For sake of the god of your choice, please always provide runnable
code as well as hints to

- what you think it should do
- what you want it to do
- what exact error message(s) you get

NOT just "it doesn't work".

Regards,


Björn
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top