How to create a class in Python
Answered
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
Bronze
•
565 rep
Your Answer
You need to be logged in to answer questions.
Log In to Answer