Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Translating some Java to Python

Reply
Thread Tools

Translating some Java to Python

 
 
Daniel Gee
Guest
Posts: n/a
 
      05-20-2007
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?

 
Reply With Quote
 
 
 
 
Arnaud Delobelle
Guest
Posts: n/a
 
      05-20-2007
On May 20, 9:24 pm, Daniel Gee <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Daniel Gee
Guest
Posts: n/a
 
      05-20-2007
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.

 
Reply With Quote
 
Herman Slagman
Guest
Posts: n/a
 
      05-21-2007

"Daniel Gee" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed) 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

 
Reply With Quote
 
Ant
Guest
Posts: n/a
 
      05-21-2007
On May 20, 9:24 pm, Daniel Gee <(E-Mail Removed)> 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/


 
Reply With Quote
 
Herman Slagman
Guest
Posts: n/a
 
      05-21-2007

"Ant" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed) 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

 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      05-21-2007
>
> 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
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      05-21-2007
En Mon, 21 May 2007 07:39:09 -0300, Unknown <(E-Mail Removed)>
escribió:

> "Ant" <(E-Mail Removed)> schreef in bericht
> news:(E-Mail Removed) 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

 
Reply With Quote
 
Herman Slagman
Guest
Posts: n/a
 
      05-21-2007

"Gabriel Genellina" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...
> En Mon, 21 May 2007 07:39:09 -0300, Unknown <(E-Mail Removed)>
> escribió:
>
>> "Ant" <(E-Mail Removed)> schreef in bericht
>> news:(E-Mail Removed) 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).


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

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      05-21-2007
En Mon, 21 May 2007 09:26:19 -0300, Unknown <(E-Mail Removed)>
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dict slice in python (translating perl to python) hofer Python 12 09-11-2008 11:46 PM
Translating Java apps into machine code dado0583@hotmail.com Java 5 09-16-2005 04:01 PM
Which is easier? Translating from C++ or from Java... cjl Python 13 03-29-2005 09:21 PM
need help translating a PHP for statement to python Davis Marques Python 6 01-12-2004 01:14 PM
Advanced (?) compiling, freezing, translating Python luszczek1@netscape.net Python 0 01-04-2004 03:47 AM



Advertisments