I'm coming from a C++ background to python
I have been declaring member variables and setting them in a C++esqe way like so:
class MyClass:
my_member = []
def __init__(self,arg_my_member):
self.my_member = arg_my_member
Then I noticed in some open source code, that the initial declaration my_member = []
was completely left out and only created in the constructor.
Which obviously is possible as python is dynamic.
My question is, is this the preferred or Pythonic way of doing things, and are there and pros and cons of either?
The way you are doing it means that you'll now have a "static" member and a "non-static" member of the same name.
class MyClass:
my_member = []
def __init__(self, arg_my_member):
self.my_member = arg_my_member
>>> a = MyClass(42)
>>> print a.my_member
42
>>> print MyClass.my_member
[]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With