How to create a class in Python

Answered
Jan 05, 2026 1836 views 1 answers
13

What is the proper way to define a class with methods and properties?

N
Asked by noor_code
Gold 255 rep

1 Answer

8
class User:
    # Class variable
    user_count = 0
    
    def __init__(self, name, email):
        self.name = name
        self.email = email
        User.user_count += 1
    
    def __str__(self):
        return f"User: {self.name}"
    
    @property
    def display_name(self):
        return self.name.title()
    
    @classmethod
    def get_count(cls):
        return cls.user_count
    
    @staticmethod
    def validate_email(email):
        return '@' in email

# Usage
user = User("john", "john@example.com")
print(user.display_name)
H
Answered by hassan_ops 1 week, 2 days ago
Bronze 565 rep

Your Answer

You need to be logged in to answer questions.

Log In to Answer

Related Questions

Hot Questions

No hot questions available.