Coming from a Java background, I want a class to have certain attributes and set them in the constructor.
Currently I use the global keyword
eg.
def class A():
global attribute1
global attribute2
__init__():
attribute1 = 1
attribute2 = 2
Is there a better way of doing this? Reason I am asking is because I am not sure whether setting all attributes “Global” is correct.
EDIT: Also, whenever I access the attributes, I need self.attribute … is this necessary? i.e. in java you dont require this.attribute all the time.
The right way to do this is to add the attributes to the constructor and add them on your instance.
As for your second question, yes, you need to use
self.attributewithin the instance to refer to it.