Static variable inheritance in Python

前端 未结 2 995
感情败类
感情败类 2021-02-04 08:12

I\'m writing Python scripts for Blender for a project, but I\'m pretty new to the language. Something I am confused about is the usage of static variables. Here is the piece of

2条回答
  •  醉酒成梦
    2021-02-04 08:29

    use type(self) for access to class attributes

    >>> class A(object):
     var  = 2
     def write(self):
      print type(self).var
    >>> class B(A):
     pass
    >>> B().write()
    2
    >>> B.var = 3
    >>> B().write()
    3
    >>> A().write()
    2
    

提交回复
热议问题