python - Why should we pass args as args and not "*args"? -


as i'm reading django documentation, see in example:

from django.db import models  class handfield(models.field):      description = "a hand of cards (bridge style)"      def __init__(self, *args, **kwargs):         kwargs['max_length'] = 104         super(handfield, self).__init__(*args, **kwargs) 

i dont understand why call super is:

super(handfield, self).__init__(*args, **kwargs) 

whereas (coming c programming) thought should be:

super(handfield, self).__init__(args, kwargs) 

how comes?

yes. , i'm not asking mean "*" , "**" (link marked duplicate), i'm asking why it's not re-sent without star = why it's re-sent parent with stars, means me: "dictionary of dictionary". , question different duplicate link, , duplicate link doesnt answer question.

the reason use */** unpack packed values given. when class initialized, might initialized with:

handfield(1, 2, 3, abc=4, xyz=5) 

because receives arguments using variable length positional (*) , dynamic keyword (**) arguments (to avoid needing remember , deal specifics of parent class constructor receives), args received (1, 2, 3) , kwargs {'abc': 4, 'xyz': 5}. if parent class defined __init__ of:

def __init__(self, a1, a2, a3, spam=6, eggs=7, abc=none, xyz=none): 

then calling super(handfield, self).__init__(args, kwargs) pass args tuple a1, kwargs dict a2, , pass nothing @ a3 or other arguments. unpacking args , kwargs, convert individual positional , keyword arguments, a1 1, a2 2, a3 3, abc 4 , xyz 5.

basically, they're inverse operations; if accept *, caller passes positional arguments 1 one , "packed" single tuple, accepting ** accepts individual keyword arguments "packed" single dict. if want pass them along same way, "unpack" them make them individual arguments, not collections of arguments.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -