I have two questions regarding this piece of code:
class Enemy
def __init__ (self, x):
self.energy=x
jason=Enemy(5)
Why do I have to use
selfwhen I create functions and instance variables? What is the purpose of using it?When we create the
jasonobject, we assign it a life of5, asEnemy(5). However, can class names take variables inside? Or is it the__init__function which makes it possible? (I'd expect something like,class Enemy (x), when we declare the class).
selfrefers the current object.jasonyou wouldn't be callingEnemy.__init__, so it wouldn't matter! Declaring a method is just like declaring a function - you define the parameters now that will get passed to it later on.