How to make class in Python

I am a beginner and was trying out to make class in python. But it seems some error comes out everytime. Help me.

In python the indentation plays an important role and determines which code goes together. As you have it currently, the harry and larry instantiation happens in the Student class definition, but at that point Student class is not yet defined.

1 Like

thank you. but how to define student class now?

Hi Prasanna7! Welcome to the community :slight_smile:

You should be looking for answers by yourself first, you learn the most that way. For example how to define a class is quite readily available information, you only have to run one google search, a quick read here Python Classes and Objects - GeeksforGeeks will answer your question. Below is an easy to digest example code for a simple class with one property and a constructor.

class Student:
    x = 5
    def __init__(self, name: str, age: int): = name
        self.age = age
s1 = Student('Harold', 18)
s2 = Student('Alex', 15)

print(   # Harold
print(s1.age)    # 18
print(s1.x)      # 5
print(   # Alex
print(s2.age)    # 15
print(s2.x)      # 5

thanks @Jacobs322 it was really helpful for me

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.