33# Static Variable Inside Functions
44# ------------------------------------
55
6- # TODO
6+ # No Native Support.
7+ # Use a function attribute if you need persistent function-local state.
8+ def my_function ():
9+ if not hasattr (my_function, " counter" ):
10+ my_function.counter = 0
711
12+ my_function.counter += 1
13+ return my_function.counter
814
915# ------------------------------------
1016# Static Member Variable Inside Class
1117# ------------------------------------
1218
13- # TODO
14-
19+ class MyClass :
20+ my_variable = 0
1521
1622# ------------------------------------
1723# Static Global Variable in a File
1824# ------------------------------------
1925
20- # TODO
21-
26+ # Use module-level variables.
27+ my_variable = 0
28+ MY_CONSTANT = 10
2229
2330# ------------------------------------
2431# Static Functions & Methods
2734# Decorator that defines a static method
2835
2936class Calculator :
30- @ staticmethod
31- def add (x , y ):
32- return x + y
37+ @ staticmethod
38+ def add (x , y ):
39+ return x + y
3340
3441# Calling Ex.:
3542print (' Product:' , Calculator.add(15 , 110 ))
@@ -38,9 +45,9 @@ print('Product:', Calculator.add(15, 110))
3845# Static Class Objects
3946# ------------------------------------
4047
41- # TODO
48+ # Python has no static class syntax.
49+ # Use a class with class attributes, a module, or an object instance.
50+ class Settings :
51+ application_name = " Syntax Reference"
4252
4353```
44-
45- > More Info:
46- > - https://realpython.com/instance-class-and-static-methods-demystified/
0 commit comments