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?