When I init a class in python, is it only called once? For example, if I write an if statement in the init area.
class hi():
def __init__():
# bla bla bla
Does it only loop over once?
__init__ is the constructor of the class. It is called once for every instance of the class you create.
class A(object):
def __init__(self):
print('__init__ called')
a1 = A() # prints: __init__ called
a2 = A() # prints: __init__ called
BTW, this is not something specific to pygame, but python in general.
The constructor always takes at least one argument: self. It can take additional arguments. self is the instance which is being constructed and you can write initialization of it in the constructor.
class A(object):
def __init__(self, value):
self.value = value
a1 = A(17)
print (a1.value) # prints: 17
The init function is ideally used to declare variables in oops style of python.
def classA:
def __init__(self):
self.somevariable = somevalue
def classB:
def __init__(self):
#again declare some variables
If you use classB, you can use both the variables of classA as well as that of classB. However if there was no init in classB then only that of classA will be used. As for your question i would say that it can run again. I assume that by looping you mean if it can be used again.
__init__is only called once for each time you instantiate the class. Having said that your question is not completely clear. if statements never loop under any conditions.