By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

Translating some Java to Python

P: n/a
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?

May 20 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On May 20, 9:24 pm, Daniel Gee <zef...@gmail.comwrote:
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

May 20 '07 #2

P: n/a
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.

May 20 '07 #3

P: n/a

"Daniel Gee" <ze****@gmail.comschreef in bericht
news:11*********************@r3g2000prh.googlegrou ps.com...
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

May 21 '07 #4

P: n/a
Ant
On May 20, 9:24 pm, Daniel Gee <zef...@gmail.comwrote:
....
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/
May 21 '07 #5

P: n/a

"Ant" <an****@gmail.comschreef in bericht
news:11**********************@r3g2000prh.googlegro ups.com...
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

May 21 '07 #6

P: n/a
>
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
May 21 '07 #7

P: n/a
En Mon, 21 May 2007 07:39:09 -0300, Unknown <un*****@unknown.invalid>
escribió:
"Ant" <an****@gmail.comschreef in bericht
news:11**********************@r3g2000prh.googlegro ups.com...
>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

May 21 '07 #8

P: n/a

"Gabriel Genellina" <ga*******@yahoo.com.arschreef in bericht
news:ma***************************************@pyt hon.org...
En Mon, 21 May 2007 07:39:09 -0300, Unknown <un*****@unknown.invalid>
escribió:
>"Ant" <an****@gmail.comschreef in bericht
news:11**********************@r3g2000prh.googlegr oups.com...
>>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

May 21 '07 #9

P: n/a
En Mon, 21 May 2007 09:26:19 -0300, Unknown <un*****@unknown.invalid>
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

May 21 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.