Subscribe to Bodom's Universe        RSS Feed
-----

Defining data members in a Python Class

Icon Leave Comment
Contrary to what was stated in my tutorial, python data members should not be initialized outside of the __init__ function. Therefore, for example, instead of having:

class MyClass:
	a = 0
	def __init__(self, _a):
		self.a = _a



You would actually have:

class MyClass:
	def __init__(self, _a):
		self.a = _a



The 'self' making the variable a a data member of the class. Otherwise, there are times where you will get incorrect output of data members, etc, as was pointed out by a very wise redditor.

Sorry if I misled anyone!

Peace,
~Bodom

0 Comments On This Entry

 

January 2022

S M T W T F S
      1
2345678
9101112131415
161718192021 22
23242526272829
3031     

Recent Entries

Recent Comments

Search My Blog

9 user(s) viewing

9 Guests
0 member(s)
0 anonymous member(s)