Home correct way to define class variables in Python
Reply: 0

correct way to define class variables in Python

user659
1#
user659 Published in July 23, 2018, 9:30 am

Possible Duplicate:
Variables inside and outside of a class __init__() function

I noticed that in Python, people initialize their class attributes in two different ways.

The first way is like this:

class MyClass:
  __element1 = 123
  __element2 = "this is Africa"

  def __init__(self):
    #pass or something else

The other style looks like:

class MyClass:
  def __init__(self):
    self.__element1 = 123
    self.__element2 = "this is Africa"

Which is the correct way to initialize class attributes?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.372346 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO