miercuri, 20 mai 2009

Python and static methods

At my new working place I had to learn Python so I discovered some features that are hard to understand for someone with Java background. One of these features is definition of static methods.

In Java, you can write something like:

public static void helloWorld() { System.out.println("Hello world"); }

In python lucrurile sunt mai complicate si depinde si de versiune folosita. De exemplu, daca folosim o versiune de python anterioara 2.4 atunci vom defini o metoda statica in felul urmator:

In Python, it's more difficult to define class/static methods. If we use a version earlier than 2.4 we will define a static method like this:

def helloWorld(cls):
print("Salutari")

helloWorld = classmethod(helloWorld)

If we use a version newer than 2.4, the implementation of static methods is simpler because we use a decorator:
@staticmethod
def helloWorld(cls):
print("Hello world")

In both cases, defining a static method in Python is harder than in Java.

Niciun comentariu:

Trimiteți un comentariu