new style class

Discussion in 'Python' started by gert, Nov 2, 2007.

  1. gert

    gert Guest

    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 ?
     
    gert, Nov 2, 2007
    #1
    1. Advertising

  2. gert

    Boris Borcic Guest

    gert wrote:
    > 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 ?
     
    Boris Borcic, Nov 2, 2007
    #2
    1. Advertising

  3. gert

    gert Guest

    On Nov 2, 12:27 pm, Boris Borcic <> wrote:
    > gert wrote:
    > > 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 ?


    I don't know I thought it was supported from 2.2?
     
    gert, Nov 2, 2007
    #3
  4. gert

    gert Guest

    On Nov 2, 12:31 pm, gert <> wrote:
    > On Nov 2, 12:27 pm, Boris Borcic <> wrote:
    >
    >
    >
    > > gert wrote:
    > > > 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 ?

    >
    > 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')
     
    gert, Nov 2, 2007
    #4
  5. gert wrote:
    > On Nov 2, 12:27 pm, Boris Borcic <> wrote:
    >> gert wrote:
    >>> 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 ?

    >
    > 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
     
    Wildemar Wildenburger, Nov 2, 2007
    #5
  6. gert

    Tim Chase Guest

    gert wrote:
    > On Nov 2, 12:27 pm, Boris Borcic <> wrote:
    >> gert wrote:
    >>> 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 ?

    >
    > 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
     
    Tim Chase, Nov 2, 2007
    #6
  7. gert

    Nigel Rantor Guest

    gert wrote:
    > On Nov 2, 12:27 pm, Boris Borcic <> wrote:
    >> gert wrote:
    >>> 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 ?

    >
    > 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
     
    Nigel Rantor, Nov 2, 2007
    #7
  8. >>>>> "gert" == gert <> writes:
    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
     
    Martin Sand Christensen, Nov 2, 2007
    #8
  9. 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')
    >



    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
     
    Wildemar Wildenburger, Nov 2, 2007
    #9
  10. gert

    Boris Borcic Guest

    gert wrote:
    [...]

    >>>> Why doesn't this new style class work in python 2.5.1 ?
    >>> why should it ?

    >> 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')
    >


    You should put a '@staticmethod' decorator before your m2 method definition
     
    Boris Borcic, Nov 2, 2007
    #10
  11. gert

    gert Guest

    Could not one of you just say "@staticmethod" for once damnit :)
     
    gert, Nov 2, 2007
    #11
  12. gert wrote:
    > Could not one of you just say "@staticmethod" for once damnit :)


    No, since everyone's crystal balls are in repair.

    Regards,


    Björn

    --
    BOFH excuse #256:

    You need to install an RTFM interface.
     
    Bjoern Schliessmann, Nov 2, 2007
    #12
  13. gert wrote:

    > 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

    --
    BOFH excuse #279:

    The static electricity routing is acting up...
     
    Bjoern Schliessmann, Nov 2, 2007
    #13
  14. Bjoern Schliessmann wrote:
    > gert wrote:
    >> Could not one of you just say "@staticmethod" for once damnit :)

    >
    > No, since everyone's crystal balls are in repair.
    >


    I don't even have crystal balls!

    /W
     
    Wildemar Wildenburger, Nov 2, 2007
    #14
  15. gert

    gert Guest

    On Nov 2, 2:10 pm, Wildemar Wildenburger
    <> wrote:
    > Bjoern Schliessmann wrote:
    > > gert wrote:
    > >> Could not one of you just say "@staticmethod" for once damnit :)

    >
    > > No, since everyone's crystal balls are in repair.

    >
    > I don't even have crystal balls!
    >
    > /W


    lol
     
    gert, Nov 2, 2007
    #15
  16. Wildemar Wildenburger wrote:
    > Bjoern Schliessmann wrote:


    >> No, since everyone's crystal balls are in repair.

    >
    > I don't even have crystal balls!


    Not many are rich *and* impotent. :)

    Regards,


    Björn

    --
    BOFH excuse #14:

    sounds like a Windows problem, try calling Microsoft support
     
    Bjoern Schliessmann, Nov 2, 2007
    #16
  17. gert

    Boris Borcic Guest

    gert wrote:
    > Could not one of you just say "@staticmethod" for once damnit :)
    >


    I did, did I not ?
     
    Boris Borcic, Nov 2, 2007
    #17
  18. gert

    Nigel Rantor Guest

    gert wrote:
    > Could not one of you just say "@staticmethod" for once damnit :)
    >


    why were you asking if you knew the answer?

    yeesh
     
    Nigel Rantor, Nov 2, 2007
    #18
  19. gert

    gert Guest

    On Nov 2, 4:04 pm, Boris Borcic <> wrote:
    > gert wrote:
    > > Could not one of you just say "@staticmethod" for once damnit :)

    >
    > I did, did I not ?


    i am sorry, yes you did :)
     
    gert, Nov 2, 2007
    #19
  20. gert

    Aahz Guest

    Aahz, Nov 2, 2007
    #20
    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. dlo_olb
    Replies:
    2
    Views:
    2,336
    Fredrik Lundh
    Jun 25, 2003
  2. ankit
    Replies:
    1
    Views:
    353
    Alex Martelli
    Dec 22, 2005
  3. looping
    Replies:
    38
    Views:
    869
    Bengt Richter
    Apr 25, 2006
  4. Ken Varn
    Replies:
    0
    Views:
    492
    Ken Varn
    Apr 26, 2004
  5. Jayden
    Replies:
    9
    Views:
    302
    alex23
    Sep 27, 2012
Loading...

Share This Page