473,503 Members | 6,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Translating some Java to Python

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
9 1297
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
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

"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
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

"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
>
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3447
by: Dave Brueck | last post by:
Below is some information I collected from a *small* project in which I wrote a Python version of a Java application. I share this info only as a data point (rather than trying to say this data...
6
1545
by: Davis Marques | last post by:
hi; I'm translating some PHP scripts to Python and have hit a roadblock with a for statement. If someone could explain to me how one should translate the multiple increment, evaluations, etc....
1
47594
by: greg.knaddison | last post by:
Hi, I'm trying to use the httpclient within Jython (see http://jakarta.apache.org/commons/httpclient/ for more information on the httpclient). My Jython version is: Jython 2.1 on...
4
3408
by: angel | last post by:
A java runtime environment includes jvm and java class (for example classes.zip in sun jre). Of course jython need jvm,but does it need java class. Thanx
2
1437
by: Henrik S. Hansen | last post by:
How do you best go about translating characters like '\\n' to '\n'? This is for a configuration file parser, where the "backslash convention" is supported. The naive approach --...
114
9691
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
13
1799
by: cjl | last post by:
Hey all: I'm working on a 'pure' python port of some existing software. Implementations of what I'm trying to accomplish are available (open source) in C++ and in Java. Which would be...
11
9211
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
12
5890
by: Mark Fink | last post by:
I wrote a Jython class that inherits from a Java class and (thats the plan) overrides one method. Everything should stay the same. If I run this nothing happens whereas if I run the Java class it...
0
7070
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.