I am new to Python having come from mainly Java programming.
I am currently pondering over how classes in Python are instantiated.
I understand that __init__()
: is like the constructor in Java. However, sometimes python classes do not have an __init__()
method which in this case I assume there is a default constructor just like in Java?
Another thing that makes the transition from Java to python slightly difficult is that in Java you have to define all the instance fields of the class with the type and sometimes an initial value. In python all of this just seems to disappear and developers can just define new fields on the fly.
For example I have come across a program like so:
class A(Command.UICommand):
FIELDS = [
Field( 'runTimeStepSummary', BOOL_TYPE)
]
def __init__(self, runTimeStepSummary=False):
self.runTimeStepSummary = runTimeStepSummary
"""Other methods"""
def execute(self, cont, result):
self.timeStepSummaries = {}
""" other code"""
The thing that confuses (and slightly irritates me) is that this A class does not have a field called timeStepSummaries yet how can a developer in the middle of a method just define a new field? or is my understanding incorrect?
So to be clear, my question is in Python can we dynamically define new fields to a class during runtime like in this example or is this timeStepSummaries variable an instance of a java like private variable?
EDIT: I am using python 2.7