Page 1 of 1

Klasse oder statische Methode - Python

Posted: 24 Dec 2024, 21:17
by Guest
Ich möchte in meinem Fall verstehen, ob ich eine statische oder eine Klassenmethode verwenden soll.
Die Idee: Ich bearbeite keine Klassenvariable, sondern verwende diese Variable.
Beispiel 1:

Code: Select all

class RandClass():
static_variable = "some rand value to static variable"

@classmethod
def func_that_return_string_contain_static_variable(cls):
return "some rand string" + cls.static_variable
Beispiel2:

Code: Select all

class RandClass():
static_variable = "some rand value to static variable"

@staticmethod
def func_that_return_string_contain_static_variable():
return "some rand string" + RandClass.static_variable
Was ist die beste Vorgehensweise?