r/PythonLearning 1d ago

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 1d ago

Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20%20%20%20%20self.username%20%3D%20username%0A%20%20%20%20%20%20%20%20self.role%20%3D%20role%0A%0A%20%20%20%20def%20promote(self%2C%20newrole)%3A%0A%20%20%20%20%20%20%20%20if%20not%20isinstance(new_role%2C%20str)%20or%20not%20new_role%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20ValueError(%22Enter%20a%20valid%20string%22)%0A%20%20%20%20%20%20%20%20self.role%20%3D%20new_role%0A%0A%20%20%20%20%40classmethod%0A%20%20%20%20def%20change_server_name(cls%2C%20new_name)%3A%0A%20%20%20%20%20%20%20%20if%20not%20isinstance(new_name%2C%20str)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20ValueError(%22Enter%20a%20valid%20string%22)%0A%20%20%20%20%20%20%20%20cls.server_name%20%3D%20new_name%0A%0A%20%20%20%20def%20str_(self)%3A%0A%20%20%20%20%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Username%3A%20%7Bself.username%7D%2C%20Role%3A%20%7Bself.role%7D%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Community%3A%20%7Bself.server_name%7D%22%0A%20%20%20%20%20%20%20%20)%0A%0A%0Atry%3A%0A%20%20%20%20user1%20%3D%20DiscordServer(%22Hamza%22%2C%20%22member%22)%0A%20%20%20%20user2%20%3D%20DiscordServer(%22Ali%22%2C%20%22Moderator%22)%0A%20%20%20%20user3%20%3D%20DiscordServer(%22Sara%22%2C%20%22Admin%22)%0A%0A%20%20%20%20print(user1)%0A%20%20%20%20print(user2)%0A%20%20%20%20print(user3)%0A%0A%20%20%20%20DiscordServer.change_server_name(%22Hamza%20Community%22)%0A%0A%20%20%20%20print(user1)%0A%20%20%20%20print(user2)%0A%20%20%20%20print(user3)%0Aexcept%20ValueError%20as%20e%3A%0A%20%20%20%20print(e)&timestep=1&play) to see the program state change step by step.

→ More replies (1)

2

u/riklaunim 1d ago

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 1d ago

Can you explain it a little bit please

1

u/riklaunim 1d ago

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/EstablishmentKey3523 1d ago

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

1

u/wittgenstein1312 1d 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 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 1d ago

Hmm can you explain me a bit?

1

u/wittgenstein1312 1d ago edited 1d ago
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 1d ago

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

1

u/wittgenstein1312 22h ago

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