Classes

I can’t understand classes in python.Really they are giving me a hard time

1 Like

I’ve just recently learned them, so someone correct me if I’m wrong…

Explanation

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.

2 Likes

1 is type int
“a” is type str
True is type bool
1.0 is type float

class myClass(x):
pass

cat = myClass(cat)

cat is type myClass
just like all the other types

Hey there @shashwatsagar and welcome to the forums :slightly_smiling_face:

@jamarc and @ahmeddeq2253266208 have given a good basic explanation, though I am guessing you may need more specific help. What about classes are you stuck on?

2 Likes

Welcome to the Codecademy Forums, @shashwatsagar and @ahmeddeq2253266208!

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).

2 Likes

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,

class Dog:
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.
Thank You.

3 Likes