Ask coding questions

← Back to all posts
Class in python

Hello, I'm new in but I am a bit experienced in python. But I am always confused when some tells me about class in python. Can some one help me with this?

Answered by InvisibleOne [earned 5 cycles]
View Answer

Sure, classes in python hold data sort of like a dictionary, but different. Basic classes look like this:

The reason why classes can seem to be complicated is because there is a special function you can make inside of a class called __init__() Inside of init we have self as the first argument, than any other arguments we want to add to our object. We can then use that class to make lots of other objects.
That description is pretty bad but this should explain it. Suppose for some reason in a game, we want to make a bunch of cars, each of these cars will have a color, model, and max_speed. Instead of making a class for each car, or a dictionary to hold the data, we can make a class using init and then reuse that class.

Now we can use that class to create other cars

That isn't really the best of explanations, but here's a repl you can take a look at if you want to see some actual code using classes.
And if you have any other questions, go ahead and ask my and I'll try to answer them.


@InvisibleOne This is really helpful actually, thank you



Python is an object oriented programming language, class in python is Something like an outline to create a new object. Class object can be used over and over again as needed.

Let's create a class named "Debbie" we will use the keyword class:

Classes have a function called as init() function to assign values to object properties.

Let's take an example code:

Output -

Explore this resource for detailed explanation of Python class: