How to pass an object in Python with the self keyword?

Question:

Is it possible to pass an object in Python such that the only reference to the object is the self keyword?

class A:
    
    def __init__(self):
        self.val = 'A'
        
    def wave(self,friend):
        friend.make_friend(self)
        
        
class B:
    
    def __init__(self):
        val = 'B'
        
    def make_friend(self, friend):
        self.friend = friend
    
a = A
b = B    

a.wave(friend=B)

>>>
----> 1 a.wave(friend=B)

TypeError: A.wave() missing 1 required positional argument: 'self'

From this error, it does not appear to be possible. See class A method wave. This method intends to send A to B. But I can’t reference A within A because it’s simply an instance of the class. Thus, I want to pass it using the self keyword.

Asked By: jbuddy_13

||

Answers:

It’s possible yes. You just happen to get that error due to not creating an instance of class A. To create an instance of a class, you need to call the class with parentheses.

a = A()
b = B()
a.wave(friend=b)
Answered By: K4nj

I think the issue in your example is that you haven’t instantiated either class. a = A is essentially saying ‘a is another name for A’, while a = A() means ‘a is an instance of A’.

When you call class methods out in the open and not on an instance of the class like that, self is not bound for you. You can make the method static, or you can create an object.

Answered By: zjp

From the Python docs:

Class instantiation uses function notation. Just pretend that the class object is a parameterless function that returns a new instance of the class.
x = MyClass() creates a new instance of the class and assigns this object to the local variable x.

In your code snippet, you are not creating instances of these classes, hence the error that self is missing.

Answered By: PKlempe

so the error was you weren’t creating the instance of the class, and you had some unused parameters

class A:
    def __init__(self):
        self.val = 'A'
        
    def wave(self, friend):
        friend.make_friend(self)
        
        
class B:
    def __init__(self):
        self.val = 'B'
        self.friend = None
        
    def make_friend(self, friend):
        self.friend = friend
    
a = A()
b = B()    

a.wave(friend=b)
print(b.friend.val) # Output: A

instead of a = A, it should be a = A() because you are making an instance of a class.

in the class B I changed the val to self.val,to make it accessible by other methods.

in the class A method wave, the parameter friend I changed it to self to make the friend of the class A an instance.

function make_friend, the friend I added self.friend = friend.

I added a print to check if the the friend is set correctly.
now compile the code and it should output A, this would show that the friend of b is a.

Answered By: Michael
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.