Python classes

My teacher taught me the syntax for a class, but I don’t understand it. Could someone explain word for word what each thing means? Correct me if I’m wrong but I think the syntax for a class is

def __ init__(self, var1, var2):
     self.var1 = var1
     self.var2 = var2

Also, why do we need self.var1 = var1 if we already declared the variables in the parameter section?

Explain word for word sounds like you need to actually need to learn how classes in Python work which is already covered in several places, e.g. on codecademy-
Or multiple other places online you could search for.

I wouldn’t say this is the syntax for a class (note that the class keyword isn’t even in your example), just a brief example of the __init__ dunder method within a class definition. What you’re doing with = name is binding the arguments you passed (var1 and var2) to any instances created by this class where self refers to the instance/object created.

If you wanted more details on the syntax of classes the docs are always a good shout-

1 Like