Doubt with parameter

1

I'm watching a video of python but I do not understand one thing.

I have the file utils.py

def code_generator(size=6, chars=string.ascii_lowercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

def create_shortcut(instance, size = 6):
    new_code = code_generator(size = size)
    Klass = instance.__class__
    qs_exists = Klass.objects.filter(shortcode=new_code).exists()
    if qs_exists:
        return create_shortcut(size = size)
    return new_code

Model.py file

def save(self, *args, **kargs):
   if self.shortcode is None or self.shortcode == "":
       self.shortcode = create_shortcut(self)
   super(KirrURL, self).save(*args, **kargs)

In the model.py file the create_shortcut(self) call is made, which means that the class itself is passed, and the size parameter, which is default 6, has not been entered.

However, here in utils.py:

 if qs_exists:
    return create_shortcut(size = size)

In the above passage would not it have to be return create_shortcut(instance, size = size) ? Why was not the instance parameter passed again?

    
asked by anonymous 01.12.2016 / 16:17

1 answer

2

This - I did not see the video with audio, but rather, that code is wrong. The instance parameter is required and is not passed - maybe it was an example of live video code while the video was made, and therefore , subject to errors.

What happens is that as the expression with error is inside an "if" it may be that in cases that it has been tested live has never run, and then the error did not appear.     

02.12.2016 / 15:38