Jessica Chambers
Jessica Chambers

Reputation: 1316

Python: call to self function not working correctly

This seems like a very simple issue, and though I have a workaround I would like to fix it nicely.

EDIT: I am using a Django system, so the groups variable is actually inherited/retrieved from a DB) I just tried to make a minimal example, but I realised that that was not conducive to solving my issue

I have a class:

class Invite(models.Model, IndexedModelMixin):

    def get_associated_groups(self):
        return self.groups

But when I call get_associated_groups elsewhere

def get_groups(resource_object):
    resource_group_objects = resource_object.get_associated_groups()

where Invite is the resource_object, this error is thrown:

get_associated_groups() missing 1 required positional argument: 'self'

My workaround currently is

resource_group_objects = resource_object.get_associated_groups(resource_object)

Why isn't the self call implicit?

Upvotes: 0

Views: 1157

Answers (1)

JPG
JPG

Reputation: 88499

where Invite is the resource_object, this error is thrown:

Here Invite is the class, it should be the class instance.

That is, You should pass the "instance of the Ivaite model/class" instead of the Invite class

invite_instance = Invite.objects.get(id=1)
get_groups(invite_instance) # calling the function

Reference
1. Python calling method in class


Example

In [3]: class Foo: 
   ...:     names = ["Tony", "George"] 
   ...:  
   ...:     def get_names(self): 
   ...:         return self.names 
   ...:  
   ...:  
   ...: def retrieve_names(foo_instance): 
   ...:     return foo_instance.get_names()                                                                                                                                                                        

In [4]:                                                                                                                                                                                                            

In [4]: retrieve_names(Foo)                                                                                                                                                                                        
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-3eb7dbe1c600> in <module>
----> 1 retrieve_names(Foo)

<ipython-input-3-dbb19979fd85> in retrieve_names(foo_instance)
      7 
      8 def retrieve_names(foo_instance):
----> 9     return foo_instance.get_names()

TypeError: get_names() missing 1 required positional argument: 'self'

In [5]:                                                                                                                                                                                                            

In [5]: foo_inst = Foo()                                                                                                                                                                                           

In [6]: retrieve_names(foo_inst)                                                                                                                                                                                   
Out[6]: ['Tony', 'George']

Upvotes: 3

Related Questions