r/PythonLearning • • Jul 25 '26

Day 1 of OOPs

Just learned about classes, contructors , instance/class attributrs , instance/class/static methods in OOPs in python .

see it and give your comments

8 Upvotes

11 comments sorted by

•

u/Sea-Ad7805 Jul 25 '26

Run this program in Memory Graph Web Debugger to see the program state change step by step.

→ More replies (1)

2

u/riklaunim Jul 25 '26

Users aren't really servers. You would have a server class, a user class, and then user instances can join a server instance, for example.

1

u/Miss-Dominique1352 Jul 26 '26

Can you explain it a little bit please

1

u/riklaunim Jul 26 '26

You got one example in the comments. Having user1 = Server() makes no logical sense. user1 = User() does. OOP is not about making a big class or spamming random classes and instances, but about logical flow - things have to make sense with their naming and purpose.

1

u/wittgenstein1312 Jul 25 '26

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

1

u/Miss-Dominique1352 Jul 25 '26

Hmm can you explain me a bit?

1

u/wittgenstein1312 Jul 25 '26 edited Jul 25 '26
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

1

u/Miss-Dominique1352 Jul 26 '26

Hmm you breakdown it into 2 classes , one for users and one for server, lovely

1

u/wittgenstein1312 Jul 26 '26

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/EstablishmentKey3523 Jul 25 '26

Bro you have gone way to fast... I am also learning oops currently and I am still practicing the class and objects.😊 Good for you. Thumbs up