sidmontu
sidmontu

Reputation: 119

Python: *args behaviour in class methods

I am trying to write a wrapper Python function that takes in a variable number of positional arguments, which are then processed and fed into a main function from a library. I've created a minimal example here that shows the issue I'm currently facing:

class bar :
    def __init__(self, *args) :
        print(args)
    def foo(self,*args) :
        print(args)

a = bar('name',123,'def')
bar.foo('name',123,'def')

The output is :

('name', 123, 'def')

(123, 'def')

It looks like 'name' is being assigned to self in foo(), but not in __init__(). Is there a way around this?

Upvotes: 0

Views: 43

Answers (1)

Dan D.
Dan D.

Reputation: 74655

You are calling the method on the class bar.foo when you should be calling the method on the instance a.foo.

a.foo('name',123,'def')

Upvotes: 1

Related Questions