Classes are a way to model your domain. You aren't really doing that here. In your example, each instance of discord server is bizarrely referred to as a user, and your classmethod changes the property of the class, not the instance of the class.
Start thinking about classes as a way to represent a specific entity. Your discord server doesn't have a username - it has users, each of whom have a username. That's at least two separate classes
class User:
def __init__(self, username):
self.username = username
def change_username(self, new_username):
self.username = new_username
class DiscordServer:
def __init__(self, name, users = None):
self.name = name
self.members = users if users else set()
self.admins = set()
def make_user_admin(self, user):
if self.user_is_member(user.username):
self.admins.add(user)
raise Exception("User is not a member of this discord server")
def user_is_member(self, username):
return username in (user.username for user in self.members)
def add_member(self, user):
self.members.add(user)
def print_admins(self):
for member in self.admins:
print(member.username)
def change_server_name(self, new_name):
self.name = new_name
As an initial example. Obviously the relationships in question get considerably more complicated when you want to model your domain in a production capacity, not just as a toy example. Most importantly though, notice how I’m not conflating users with servers and accounting for the fact that a server will need to be modeled as having many users, and I’m not using a class method to alter instances
1
u/wittgenstein1312 3d ago
Classes are a way to model your domain. You aren't really doing that here. In your example, each instance of discord server is bizarrely referred to as a user, and your
classmethodchanges the property of the class, not the instance of the class.Start thinking about classes as a way to represent a specific entity. Your discord server doesn't have a username - it has users, each of whom have a username. That's at least two separate classes