Various places to declare Static variables.

  • In general, we can declare within the class directly but from outside of my method.
  • Inside constructor by using class name
  • Inside instance method by using class name.
  • Inside class method by using either class name or class variable.
  • Inside static method by using class name.


class Test:
    a=10
    def __init__(self):
        Test.b=20
    def m1(self):
        Test.c=30
    @classmethod
    def m2(cls):
        cls.d1=40
        Test.d2=400
    @staticmethod
    def m3():
        Test.e=50
    print(Test.__dict__)
    t=Test()
    print(Test.__dict__)
    t.m1()
    print(Test.__dict__)
    t.m2()
    print(Test.__dict__)
    t.m3()
    print(Test.__dict__)
    Test.f=60
    print(Test.__dict__)        

要查看或添加评论,请登录

Rohit Shrimangle的更多文章

社区洞察

其他会员也浏览了