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?

Jessica Chambers
  • 1,246
  • 5
  • 28
  • 56
  • 2
    `def __init__(self): self.groups = ...` https://www.w3schools.com/python/python_classes.asp – Max Apr 24 '19 at 08:15
  • @DeepSpace I edited the question, though maybe it's still the same issue? I can't really use `__init__` with Django – Jessica Chambers Apr 24 '19 at 08: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` – DeepSpace Apr 24 '19 at 08:31

1 Answers1

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']
JPG
  • 82,442
  • 19
  • 127
  • 206