Baza wiedzyCentrum egzaminacyjne
PLPolski
Wybierz język13

CKTIS / Python

staticmethod()

Udostępnia funkcję w przestrzeni nazw klasy bez automatycznego dodawania self lub cls.

Wersja: Python 3.14; repository image, production runtime not attested

Składnia

@staticmethod
def double(value):
    return 2 * value

Udostępnia funkcję w przestrzeni nazw klasy bez automatycznego dodawania self lub cls.

Przy wywołaniu przez klasę do double trafia tylko 3, co daje 6. Przez instancję trafia tylko 4, co daje 8.

Parametry zadeklarowane w samej funkcji podaje się jawnie.

class Meter:
    @staticmethod
    def double(n): return n*2
assert Meter.double is Meter().double and Meter.double(3) == 6

Przykłady

class Meter:
    @staticmethod
    def double(value):
        return 2 * value

print(Meter.double(3), Meter().double(4))

Wynik

6 8

Parametry zadeklarowane w samej funkcji podaje się jawnie.

Źródła