correct way to define class variables in Python

ID : 10214

viewed : 28

Tags : pythonclassattributespython

Top 5 Answer for correct way to define class variables in Python

vote vote

99

Neither way is necessarily correct or incorrect, they are just two different kinds of class elements:

  • Elements outside the __init__ method are static elements; they belong to the class.
  • Elements inside the __init__ method are elements of the object (self); they don't belong to the class.

You'll see it more clearly with some code:

class MyClass:     static_elem = 123      def __init__(self):         self.object_elem = 456  c1 = MyClass() c2 = MyClass()  # Initial values of both elements >>> print c1.static_elem, c1.object_elem  123 456 >>> print c2.static_elem, c2.object_elem 123 456  # Nothing new so far ...  # Let's try changing the static element MyClass.static_elem = 999  >>> print c1.static_elem, c1.object_elem 999 456 >>> print c2.static_elem, c2.object_elem 999 456  # Now, let's try changing the object element c1.object_elem = 888  >>> print c1.static_elem, c1.object_elem 999 888 >>> print c2.static_elem, c2.object_elem 999 456 

As you can see, when we changed the class element, it changed for both objects. But, when we changed the object element, the other object remained unchanged.

vote vote

86

I think this sample explains the difference between the styles:

james@bodacious-wired:~$cat test.py  #!/usr/bin/env python  class MyClass:     element1 = "Hello"      def __init__(self):         self.element2 = "World"  obj = MyClass()  print dir(MyClass) print "--" print dir(obj) print "--" print obj.element1  print obj.element2 print MyClass.element1 + " " + MyClass.element2 james@bodacious-wired:~$./test.py  ['__doc__', '__init__', '__module__', 'element1'] -- ['__doc__', '__init__', '__module__', 'element1', 'element2'] -- Hello World Hello Traceback (most recent call last):   File "./test.py", line 17, in <module>     print MyClass.element2 AttributeError: class MyClass has no attribute 'element2' 

element1 is bound to the class, element2 is bound to an instance of the class.

vote vote

79

as suggested in git status,

Unmerged paths:                                                                                                                                 (use "git add <file>..." to mark resolution)                                                                                                       both modified:   a.jl                                       both modified:   b.jl 

I used git add to finish the merging, then git checkout works fine.

vote vote

61

my issue was (master|REBASE 1/1)

this command worked for me

 git rebase --skip 
vote vote

50

I had the same issue when switching from a dev branch to master branch. What I did was commit my changes and switch to the master branch. You might have uncommitted changes.

And also you can try this git reset --merge as well. This can be used to resolve any conflicts and revert.

Top 3 video Explaining correct way to define class variables in Python

Related QUESTION?