I can’t understand classes in python.Really they are giving me a hard time
I’ve just recently learned them, so someone correct me if I’m wrong…
They’re for defining a type of object. Python objects can have both function and data elements. Python classes define what methods can be used to change the state of an object . They also indicate what attributes the object can have.
Self is used in methods to represent the instance of the class. With this keyword, you can access the attributes and methods of the class in python. It binds the attributes with the given arguments.
1 is type int
“a” is type str
True is type bool
1.0 is type float
cat = myClass(cat)
cat is type myClass
just like all the other types
Hey there @shashwatsagar and welcome to the forums
The following page provides useful advice regarding how to format code for posting on the Forums: How to ask good questions (and get good answers).
A class is an blueprint for an object. Int, Str, list, etc are all pre-defined classes in python. so everytime you write something like this,
name = "Shashwat"
You instantiate or create an instance of class str and that instance in python is referred as an object. This object now has the blueprint made for every string type object. For example like every other string it also have the same methods.
For user defined classes making an instance is a bit different. See this,
is_mammal = True
So now everytime you create an instance of dog class it will have an attribute (class variable)
is_mammal which is set by default to True.
Classes are used to implement real-life objects. For example, Student, Restaurants, Animal, etc.
I hope this introduction helps. The classes section for python at codecademy is a bit difficult to understand but you can reach out at the forums anytime you have a problem.