static method feature

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rashkatsa

    static method feature

    Hi all,

    do you know why python development team decided to forbid polymorphism
    for static methods ? As you can do it in another languages (Java,...) it
    could be very handy if you can create utility classes with static
    methods that could be differentiate from the number of parameters.

    with no static methods, it is already possible in python :

    class Assert:
    def assertEquals(se lf,expected,act ual):
    ...
    def assertEquals(se lf,msg,expected ,actual):
    ...

    but as soon as you use static :
    class Assert:
    def assertEquals(se lf,expected,act ual):
    ...
    assertEquals=st aticmethod(asse rtEquals)
    def assertEquals(se lf,msg,expected ,actual):
    ...
    # here you can't declare agin the same
    # assignment : assertEquals=st aticmethod(asse rtEquals)
    # because they only differ from the number of parameters

    several design solutions :

    1) It could be possible to extend staticmethod(fu nction) to
    staticmethod(fu nction, arity) and resolve during call with the len of
    arguments pass to the function (we could imagine that default parameter
    are not allowed : it is the price for the speed :))
    2) You could add a new key in the language (defstatic for example) like
    in the following :

    class Assert:
    defstatic assertEquals(ex pected,actual):
    ...
    defstatic assertEquals(ms g,expected,actu al):
    ...
    but you will now have two keywords for methods definition
    3) The best seems to add a method attribute like static before def keyword.

    what is your opinion ?

    regards,

    rashkatsa.

  • Duncan Booth

    #2
    Re: static method feature

    rashkatsa <rashkatsa@wana doo.fr> wrote in
    news:bpvu34$i7s $1@news-reader3.wanadoo .fr:
    [color=blue]
    > Hi all,
    >
    > do you know why python development team decided to forbid polymorphism
    > for static methods ? As you can do it in another languages (Java,...)
    > it could be very handy if you can create utility classes with static
    > methods that could be differentiate from the number of parameters.
    >
    > with no static methods, it is already possible in python :
    >
    > class Assert:
    > def assertEquals(se lf,expected,act ual):
    > ...
    > def assertEquals(se lf,msg,expected ,actual):
    > ...[/color]

    Not in any version of Python that I have ever seen. If you try this, then
    the second method will simply overwrite the first one. If you want
    polymorphism you need to look at the actual arguments passed to your method
    and adapt your processing accordingly.

    In this case a less confusing thing to do is to make the optional msg
    parameter the last parameter and give it a default. This is how unittest
    implements assertEquals.

    You can, of course, write a wrapper to dispatch methods based on argument
    types, but this tends to be messy and not usually needed.

    --
    Duncan Booth duncan@rcp.co.u k
    int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
    "\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?

    Comment

    • anton muhin

      #3
      Re: static method feature

      rashkatsa wrote:[color=blue]
      > Hi all,
      >
      > do you know why python development team decided to forbid polymorphism
      > for static methods ? As you can do it in another languages (Java,...) it
      > could be very handy if you can create utility classes with static
      > methods that could be differentiate from the number of parameters.[/color]
      It's not polymorphism, but overloading
      [color=blue]
      >
      > with no static methods, it is already possible in python :
      >
      > class Assert:
      > def assertEquals(se lf,expected,act ual):
      > ...
      > def assertEquals(se lf,msg,expected ,actual):[/color]
      No, you just *redefine* assertEquals method

      Try Assert().assert Equals('expecte d', 'actual')

      and Python'd complain:
      TypeError: assertEquals() takes exactly 4 arguments (3 given)

      [skipped]

      Python doesn't support overloading (search the group for more info on it).

      It can be emulated to some extened with default values and *args, **args

      E.g.:

      def foo(*args):
      if len(args) == 3:
      return foo3(*args)
      else:
      return default_foo(*ar gs)

      A nice example of dispatch on passed parameters is multimethod.py (you
      can google for it).

      regards,
      anton.

      Comment

      • Diez B. Roggisch

        #4
        Re: static method feature

        Hi,
        [color=blue]
        > do you know why python development team decided to forbid polymorphism
        > for static methods ? As you can do it in another languages (Java,...) it
        > could be very handy if you can create utility classes with static
        > methods that could be differentiate from the number of parameters.
        >
        > with no static methods, it is already possible in python :
        >
        > class Assert:
        > def assertEquals(se lf,expected,act ual):
        > ...
        > def assertEquals(se lf,msg,expected ,actual):
        > ...[/color]

        Have you tried to make this run? It gives me this:

        Traceback (most recent call last):
        File "/tmp/test.py", line 19, in ?
        ass.assertEqual s(1,2)
        TypeError: assertEquals() takes exactly 4 arguments (3 given)

        when trying to invoke the first method.

        Its not possible to have polymorphic methods in python at all - but you can
        do this:

        class Foo:
        def bar(_, *args):
        if len(args) == 2:
        pass
        elif len(args) == 3:
        pass

        This should also work with your static methods. Of course you can think of
        more elaborated ways of dispatching, e.g. based on type(s) and so on. This
        shall just give you the idea.

        For keyword-args, you use **kwargs. And OTOH you can use similar syntax to
        invoke a method with a list of args:

        def baz(one, two, three):
        pass

        args = [1, 2, 3]

        baz(*args)

        Regards,

        Diez

        Comment

        Working...