by Guest » 24 Dec 2024, 21:17
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?
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]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
[/code]
Beispiel2:
[code]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
[/code]
Was ist die beste Vorgehensweise?