Translating some Java to Python
Herman Slagman
herman_slagman at placid-dash-sky-dot-org
Mon May 21 08:26:19 EDT 2007
"Gabriel Genellina" <gagsl-py2 at yahoo.com.ar> schreef in bericht
news:mailman.7960.1179746966.32031.python-list at python.org...
> En Mon, 21 May 2007 07:39:09 -0300, Unknown <unknown at unknown.invalid>
> escribió:
>
>> "Ant" <antroy at gmail.com> schreef in bericht
>> news:1179738814.282655.192700 at r3g2000prh.googlegroups.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
More information about the Python-list
mailing list