Translating some Java to Python

Discussion in 'Python' started by Daniel Gee, May 20, 2007.

  1. Daniel Gee

    Daniel Gee Guest

    A while ago I wrote a class in Java for all kinds of dice rolling
    methods, as many sides as you want, as many dice as you want, only
    count values above or below some number in the total, things like
    that. Now I'm writing a project in Python that needs to be able to
    make use of that kind of a class.

    The Java version has static methods for common roll styles (XdY and XdY
    +Z) for classes that just want a result but don't want to bother
    keeping an object around for later.

    So the question is, assuming that I wanted to keep the static method
    behavior (which I'm not really sure I do), how does one format the
    method header on a 'static' method in Python? Is it just something
    like:

    class Foo:
    def statAdd(self,a):
    return a+5

    or do you drop the 'self' bit and just use a 1 variable parameter list?
     
    Daniel Gee, May 20, 2007
    #1
    1. Advertising

  2. On May 20, 9:24 pm, Daniel Gee <> wrote:
    > A while ago I wrote a class in Java for all kinds of dice rolling
    > methods, as many sides as you want, as many dice as you want, only
    > count values above or below some number in the total, things like
    > that. Now I'm writing a project in Python that needs to be able to
    > make use of that kind of a class.
    >
    > The Java version has static methods for common roll styles (XdY and XdY
    > +Z) for classes that just want a result but don't want to bother
    > keeping an object around for later.
    >
    > So the question is, assuming that I wanted to keep the static method
    > behavior (which I'm not really sure I do), how does one format the
    > method header on a 'static' method in Python? Is it just something
    > like:
    >
    > class Foo:
    > def statAdd(self,a):
    > return a+5
    >
    > or do you drop the 'self' bit and just use a 1 variable parameter list?


    Do you really want your dice rolling functions to be methods? It
    seems to me that you would be better off grouping them as functions in
    a module rather than as methods in a class. A java class full of
    static methods translates to a python module populated with functions
    in general.

    --
    Arnaud
     
    Arnaud Delobelle, May 20, 2007
    #2
    1. Advertising

  3. Daniel Gee

    Daniel Gee Guest

    Alright, sounds good. I'm just not as familiar with the preferred
    designs of python.

    As to wanting to have them in a class, sometimes I do. Persisting a
    roll in a class is only for the slightly more complicated rolls such
    as 3d6+5d4-1d12 or "4d6 (drop the lowest and re-roll ones)", things of
    that sort.
     
    Daniel Gee, May 20, 2007
    #3
  4. "Daniel Gee" <> schreef in bericht
    news:...

    > class Foo:
    > def statAdd(self,a):
    > return a+5
    >
    > or do you drop the 'self' bit and just use a 1 variable parameter list?


    class Foo:
    @staticmethod
    def statAdd(a):
    return a+5

    HTH

    Herman
     
    Herman Slagman, May 21, 2007
    #4
  5. Daniel Gee

    Ant Guest

    On May 20, 9:24 pm, Daniel Gee <> wrote:
    ....
    > The Java version has static methods for common roll styles (XdY and XdY
    > +Z) for classes that just want a result but don't want to bother
    > keeping an object around for later.
    >
    > So the question is, assuming that I wanted to keep the static method
    > behavior (which I'm not really sure I do), how does one format the
    > method header on a 'static' method in Python? Is it just something
    > like:
    >
    > class Foo:
    > def statAdd(self,a):
    > return a+5
    >
    > or do you drop the 'self' bit and just use a 1 variable parameter list?


    Herman has shown you *how* to do static methods in Python, but
    typically they are not used. Since Python has first class functions,
    and they can be defined at the module level, there is no need to use
    static methods.

    There is a distinction in Python not present in Java between class and
    static methods, class methods get the class passed in as the first
    parameter, and can be useful if you want a function that acts on data
    that the class provides. Static methods in Python are merely normal
    functions that are associated with the class - for conceptual reasons
    rather than practical ones, and are rarely used as far as I can tell.
    I've certainly never bothered with them.

    In the case of your statAdd above, self is never used in the method
    body, and so this is a clear indication that a module level function
    would be more appropriate.

    --
    Ant...

    http://antroy.blogspot.com/
     
    Ant, May 21, 2007
    #5
  6. "Ant" <> schreef in bericht
    news:...

    > Herman has shown you *how* to do static methods in Python, but
    > typically they are not used. Since Python has first class functions,
    > and they can be defined at the module level, there is no need to use
    > static methods.


    Hmm,

    As an experienced developer I'm rather new to Python, so please forgive me
    any non-Pythonic babbling.
    From a language point you're probably right, but from a design point I'd
    like to have methods that are clearly associated with a class as methods of
    that class, even if they don't use any class or instance related data.

    Herman
     
    Herman Slagman, May 21, 2007
    #6
  7. >
    > Hmm,
    >
    > As an experienced developer I'm rather new to Python, so please forgive me
    > any non-Pythonic babbling.
    > From a language point you're probably right, but from a design point I'd
    > like to have methods that are clearly associated with a class as methods
    > of that class, even if they don't use any class or instance related data.


    Why so? If they _don't_ use that class, whatfor? I use classmethods for
    factory-methods. But that at least needs the _class_ as argument somehow,
    and grouping it namespace-wise below the class makes sense.

    Otherwise, all they do is allowing for java-programming in python - and that
    simply because java lacks module level functions.

    Diez
     
    Diez B. Roggisch, May 21, 2007
    #7
  8. En Mon, 21 May 2007 07:39:09 -0300, Unknown <>
    escribió:

    > "Ant" <> schreef in bericht
    > news:...
    >
    >> Herman has shown you *how* to do static methods in Python, but
    >> typically they are not used. Since Python has first class functions,
    >> and they can be defined at the module level, there is no need to use
    >> static methods.

    >
    > As an experienced developer I'm rather new to Python, so please forgive
    > me any non-Pythonic babbling.
    >> From a language point you're probably right, but from a design point I'd

    > like to have methods that are clearly associated with a class as methods
    > of that class, even if they don't use any class or instance related data.


    In that case you might want to revise the design, perhaps you carry some
    preconceptions about how things should be, that are not directly
    applicable here. (I'm not saying this is related to your specific problem
    because I don't know exactly what you want, but in general, a lot of
    design patterns *implementations* are not directly applicable to Python).
    Modules are objects too - they're a good example of singletons. If you
    want to create a class containing only static methods: use a module
    instead. If you want to create a class having a single instance (a
    singleton), most of the time you can use a module instead.
    Functions don't *have* to be methods in a class, and the resulting design
    may still be a good design from an OO point of view.

    --
    Gabriel Genellina
     
    Gabriel Genellina, May 21, 2007
    #8
  9. "Gabriel Genellina" <> schreef in bericht
    news:...
    > En Mon, 21 May 2007 07:39:09 -0300, Unknown <>
    > escribió:
    >
    >> "Ant" <> schreef in bericht
    >> news:...
    >>
    >>> Herman has shown you *how* to do static methods in Python, but
    >>> typically they are not used. Since Python has first class functions,
    >>> and they can be defined at the module level, there is no need to use
    >>> static methods.

    >>
    >> As an experienced developer I'm rather new to Python, so please forgive
    >> me any non-Pythonic babbling.
    >>> From a language point you're probably right, but from a design point I'd

    >> like to have methods that are clearly associated with a class as methods
    >> of that class, even if they don't use any class or instance related data.

    >
    > In that case you might want to revise the design, perhaps you carry some
    > preconceptions about how things should be, that are not directly
    > applicable here. (I'm not saying this is related to your specific problem
    > because I don't know exactly what you want, but in general, a lot of
    > design patterns *implementations* are not directly applicable to Python).


    I don't really have problems with Python (yet), merely responding to the OPs
    question.

    One example that comes to mind is a class that is a proxy for a database
    class, say Person.
    The Person.Load(id) method doesn't use any instance or class data, it
    instantiates a Person and populates it from the database.
    It is clearly a part of the class's interface so for that I use a
    @staticmethod.

    > Modules are objects too - they're a good example of singletons. If you
    > want to create a class containing only static methods: use a module
    > instead. If you want to create a class having a single instance (a
    > singleton), most of the time you can use a module instead.
    > Functions don't *have* to be methods in a class, and the resulting design
    > may still be a good design from an OO point of view.


    *That* Pythonic I'm already ;-)
    For a utility 'class' I'm using a module, no need for a class there.
    Using a module for a Singleton is good tip though.

    Herman
     
    Herman Slagman, May 21, 2007
    #9
  10. En Mon, 21 May 2007 09:26:19 -0300, Unknown <>
    escribió:

    > One example that comes to mind is a class that is a proxy for a database
    > class, say Person.
    > The Person.Load(id) method doesn't use any instance or class data, it
    > instantiates a Person and populates it from the database.
    > It is clearly a part of the class's interface so for that I use a
    > @staticmethod.


    This is called a "factory method" and is usually implemented as a
    classmethod but a staticmethod is often OK.

    > *That* Pythonic I'm already ;-)


    Good!

    --
    Gabriel Genellina
     
    Gabriel Genellina, May 21, 2007
    #10
    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:
    5
    Views:
    432
  2. Replies:
    0
    Views:
    272
  3. Davis Marques
    Replies:
    6
    Views:
    317
    Paul Rubin
    Jan 12, 2004
  4. cjl
    Replies:
    13
    Views:
    483
    Peter Herndon
    Mar 29, 2005
  5. hofer
    Replies:
    12
    Views:
    1,517
Loading...

Share This Page