0

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?

3
  • 2
    def __init__(self): self.groups = ... w3schools.com/python/python_classes.asp Commented Apr 24, 2019 at 8:15
  • @DeepSpace I edited the question, though maybe it's still the same issue? I can't really use __init__ with Django Commented Apr 24, 2019 at 8:20
  • 1
    It is most likely that resource_object is not an instance of Invite but a reference to the Invite class itself. Please do print(type(resource_object)) and verify the way you instantiate resource_object Commented Apr 24, 2019 at 8:31

1 Answer 1

3

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']
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.