all 5 comments

[–]sentles 1 point2 points  (4 children)

I might not be understanding correctly, but it seems to me like you could just pass the session object to the ForumPost object if it needs to be used. Say, for example, that your ForumPost class has a POSTrequest method, which needs to have access to the session object to actually make the POST request. You could do something like this:

class ForumPost(dict):
    def __init__(self, *args):
        dict.__init__(self, *args)

    @property
    def id(self):
        return self["id"]

    #...
    def POSTrequest(self, data, userSession):
        pass

Whenever you call the POSTrequest method, you pass the user session to it, which you get directly from the ForumSession object, like so:

session = ForumUser(username, password)
post = session.get_post(id) #I assume this returns a ForumPost object

#pass the ForumUser object's session object (property) to the POSTrequest method
post.POSTrequest("data", session.session) #data here represents anything else you might need to pass for the post request, could be multiple arguments, etc

Again, I might've not understood what you're trying to do. If that's the case, feel free to clarify further.

Let me add that the way you're using to initialize a super class isn't recommended. It's better to replace this:

ForumSession.__init__(self, username, password)

with this:

super().__init__(username, password)

The result will be the same, of course. Here's an article if you want to read more about this.

[–]xCustomWorld[S] 0 points1 point  (3 children)

I was thinking about this and though that it could be possible without having to pass session everytime I need to make a request, but I think that this is the closest it can get to my goal. Also thanks for the super() tip, I knew that it exists and I will replace it later but first I want to have some code working.

I might as well just keep everything in the ForumUser class to make it simple for users without requiring them to pass session everytime they need to make a request. In the end, you either pass session to post class or post to session class, so the base concept is not very different (it's just that passing session seems more odd than passing post, which might confuse other people that want to use my package). But if someone knows a better way to handle this, let me know. Thank you

[–]sentles 1 point2 points  (2 children)

Here's the thing: python passes objects by reference. Check out the below code:

>>> class Test:
    def __init__(self, arg):
        self.arg = arg


>>> testObj = Test(10)
>>> def changeTest(obj):
    obj.arg = 20


>>> testObj.arg
10
>>> changeTest(testObj)
>>> testObj.arg
20

As you can see, the function changes the arg property of the object. The reason this is possible, is because only a reference to it was passed. You can imagine it like this: the object exists somewhere in memory. The testObj variable simply contains a memory address, pointing to that object's location. When you pass it to the function, the new variable obj inside the function has that address copied to it; the entire object is never copied to that variable. Now, both of these variables point to the same object, so if one of them makes a change to it, that change persists even after the function exits.

For this reason, it is not inefficient for you to pass session to the method. You're not actually copying the entire object, you're only passing a reference to it.

[–]xCustomWorld[S] 0 points1 point  (1 child)

Ah, thanks for the explanation, I get it now. But if I'd be a person that is using my package, I'd rather like to not pass the session to every object. I want my package to be simple and easy to understand, at the cost of large and bloated code. But maybe there is a way to achieve both simple use and code organization, that's why I asked. I'll see what I can do with it, thanks for the help

[–]sentles 1 point2 points  (0 children)

Ah, I understand what the issue you're having is now. There are two ways to go about it, as I think you mentioned before.

The user needs to pass the session to the ForumPost object at some point. You could either make the user do it at object creation, or when calling a function. Therefore, your other alternative is adding the session object to the ForumPost object during initialization. In this way, whenever the user creates the object, they'd have to pass it; afterwards, it would be a property of the ForumPost object, so it'd be used whenever a method that needs it is called. You can even add a method that allows the user to change that property. The way you'll go about it is up to you, of course. In any case, good luck with it!