Home python Method for transferring arguments for function and methods Python

Method for transferring arguments for function and methods Python




Friends, especially those who program on Python, would like to discuss the next question, which, as it seems to me to a greater extent refers to the issues of beginners, but still. I would like to hear some explanations or recommendations: in which case, what method of transferring arguments when calling a function or method is better to use and / or use you in your projects. And how do you decide how to declare a function at all. Below is a small example:

def foo (id, name, age, email):
  Print ID
  Print Name.
  Print Age.
  Print email
foo (101, 'Max', 19, '[email protected]')
Print '------'
foo (id = 101, name = 'max', age = 19, email='[email protected] ')
Print '------'
Def Bar (** kargs):
  Print Kargs ['id']
  Print Kargs ['Name']
  Print Kargs ['Email']
bar (id = 101, name = 'max', age = 19, email='[email protected] ')

foo function is declared in a standard way with arguments ID , name , age , email .
BAR function is a bit different, more precisely, the way of transferring arguments here using Kargs (about * ARGS I also know, but I decided not to give here as an example)
The most banal recommendation that follows from my logic is: when it is not clear the number of function arguments (it would be great to bring examples from real projects when so) or a lot of arguments – it is better to use ** kargs. But at the same time, it seems to me there is one minus, in terms of self-documenting of the code. Those. To understand which arguments you can pass into the function you need to be written to docting (or simply third-party documentation) or study the implementation of the function and on the basis of it already draw conclusions that it is possible to transmit, and what is impossible. I apologize for some missing question, but I hope the essence of what I ask I outlined.

Answer 1

Actually Args and Kargs I use only the command line parsing parsing (well, it is also clear). The rest of the functions make exclusively as foo (maybe I simply did not have functions with a huge number of parameters).

Def Collect_Object (Client, Param_Object, View, Args, Wait_Thread = None, stdin_passwd = false):

Although no, I even found this:


It helps when changing the parameters, since I will not miss these parameters when the function is called. Yes, and visuality, self-documenting. And about “when it is not clear the number of function arguments” – I did not have this yet. In addition, many IDEs suggest which parameters are not used, which is also sometimes useful =)

Answer 2

There are cases of use and * Args and ** kwargs . With the go I remember only Django – there is a request to the database through kwargs.

users.Objects.Filter (name = 'Ivan', Birdthday__year = 1985)

naturally Filter feature gets arguments via * args and ** kwargs , and, as I remember, simultaneously and through * Args and via ** kwargs . I do not remember how accurately done there.

What about the difference

foo (101, 'max', 19, '[email protected]')


foo (id = 101, name = 'max', age = 19, email='[email protected] ')

The second method makes sense when the arguments are predetermined. For example:

def foo (id = 0, name = none, age = 20, email = none):
  # Some Magic.
Foo (Name = "Max", email = "[email protected]")

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions