Translating some Java to Python

D

Daniel Gee

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?
 
A

Arnaud Delobelle

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.
 
D

Daniel Gee

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.
 
H

Herman Slagman

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
 
A

Ant

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.
 
H

Herman Slagman

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
 
D

Diez B. Roggisch

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
 
G

Gabriel Genellina

As an experienced developer I'm rather new to Python, so please forgive
me any non-Pythonic babbling.
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.
 
H

Herman Slagman

Gabriel Genellina said:
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
 
G

Gabriel Genellina

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!
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top