xxxxxxxxxx
>>> class MyClass:
i = 3
>>> MyClass.i
3
xxxxxxxxxx
#To create a static method, just add "@staticmethod" before defining it.
>>>class Calculator:
# create static method
@staticmethod
def multiplyNums(x, y):
return x * y
>>>print('Product:', Calculator.multiplyNums(15, 110))
Product:1650
xxxxxxxxxx
class Person:
country = "India" #This is a static variable
nationality = "Indian"
def __init__(self, name):
self.name = name
#access class variable from the class itself
print(Person.country)
#Access class variables from class instance.
a = Person("Sanjay")
print(a.name)
print(a.nationality)
xxxxxxxxxx
class Person:
country = "India" #This is a static variable
nationality = "Indian"
def __init__(self, name):
self.name = name
#access class variable from the class itself
print(Person.country)
#Access class variables from class instance.
a = Person("Sanjay")
print(a.name)
print(a.nationality)