Learning Python Classes
stadham (0)

Please help: I need the dummies tutorial on how to best understand Python Classes. I can't seem to get my head around them.

You are viewing a single comment. View All
ash15khng (393)

There are many tutorials out there, including this.

However, I'll try to explain a bit.
So, classes is kind of like a blueprint for some data.

class Animal:
    is_animal = True
    def __init__(self, species):
        self.species = species

# To create an instance of a class:
cat1 = Animal("cat")
# So now you can access stuff in it with dot notation:
print(cat1.is_animal) # prints True
print(cat1.species) # prints "cat"

So what is the __init__() function? It is basically what is called when you call Animal("cat"). You may be wondering why I passed only "cat" into the function when the function requires two arguments (self and species). That is because self is a special argument that passes the instance of the class. In our example above, cat1 is an instance of our Animal class.

I really hope this helps, but if you still have questions, ask away! I'll try and answer as soon as possible.

stadham (0)

@ash15khng Thanks that is a start.