An expanding class of the Python class decorator class calls recursion

I am overwriting the save method ModelForm, and I don't know why this would cause recursion:

@parsleyfy
class AccountForm(forms.ModelForm):
    def save(self, *args, **kwargs):
        # some other code...
        return super(AccountForm, self).save(*args,**kwargs)

Invokes the following:

maximum recursion depth exceeded while calling a Python object

Stacktrace shows this line repeating a call to itself:

return super(AccountForm, self).save(*args,**kwargs) 

Now the parsley decorator looks like this:

def parsleyfy(klass):
    class ParsleyClass(klass):
      # some code here to add more stuff to the class
    return ParsleyClass

As @DanielRoseman suggested that the Petritsa decorator extending AccountFormforces super(AccountForm,self)me to keep calling myself what the solution is?

Also, I cannot understand why this will lead to recursion.

+5
source share
2 answers

What you can do is simply call the parent method directly:

@parsleyfy
class AccountForm(forms.ModelForm):
    def save(self, *args, **kwargs):
        # some other code...
        return forms.ModelForm.save(self, *args,**kwargs)

, . , @:

class AccountFormBase(forms.ModelForm):
    def save(self, *args, **kwargs):
        # some other code...
        return super(AccountFormBase, self).save(*args,**kwargs)

AccountForm = parsleyfy(AccountFormBase)

, , - , Django.


, , , .

. Foo, , . save, super(AccountForm, self).save(...).

, , Bar, Foo. , Bar.save Foo.save - super(AccountForm, self).save(...). .

(Bar) AccountForm.

, AccountForm, Bar. .save(...) , Bar.save, Foo.save, Foo .

, Foo.save super(AccountForm, self).save(...). , - AccountForm Foo, Bar - Bar parent Foo.

, Foo.save AccountForm parent, ... Foo. , .save(...) , , .

+6

, , parsleyfy, save, :

def parsleyfy(klass):
    class ParsleyClass(klass):
        def save(self, *args, **kwargs):
            return super(klass, self).save(*args, **kwargs)
    return ParsleyClass

AccountForm :

@parsleyfy
class AccountForm(forms.ModelForm):
    def save(self, *args, **kwargs):
        return super(forms.ModelForm, self).save(*args,**kwargs)

, , - super(Class, self) vs super(Parent, self) question

0

All Articles