


Python implements a function that accepts any number of parameters
The following is a Python implementation of a function method that accepts any number of parameters. It has a good reference value and I hope it will be helpful to everyone. Let's come and take a look.
This function is not a function that I urgently need, but I just happened to see it and thought I might use it in the future. The function is to implement the function to accept different numbers of parameters.
In fact, this function is familiar in C language, although the implementation form is different. The main function in C language can implement similar functions. In this way, a program that supports command line parameters can be implemented.
First write a python demonstration code to implement the corresponding function:
defFuncDemo(*par): print("number of pars: %d" %len(par)) print("type of par: %s" %type(par)) i = 0 if len(par) != 0: for p in par: i = i + 1 print("%d par is:%s" %(i,p))
Run the test interaction record after loading:
>>>FuncDemo() number of pars: 0 type of par:<class 'tuple'> >>>FuncDemo(1,2,3) number of pars: 3 type of par:<class 'tuple'> 1 par is: 1 2 par is: 2 3 par is: 3 >>>FuncDemo(1,2,3,'abc') number of pars: 4 type of par:<class 'tuple'> 1 par is: 1 2 par is: 2 3 par is: 3 4 par is: abc
This is basically the method and application of Python to implement functions that accept arbitrary parameters , then summarize the corresponding knowledge.
Implementing a function in Python that accepts any number of parameters is relatively simple in form. Just add an asterisk in front of the parameter so that the corresponding parameter position can accept any number of parameters. The corresponding parameter is a tuple in the function, which can also be seen from the above interaction results.
In fact, this function can also support the passing of a dictionary. If a dictionary is passed in, then pairs of parameters need to be passed in.
For the time being, this function is not very useful in my work and life. Let’s see how effective it is as a backup function!
Related recommendations:
Python implementation of finding the longest non-repeating substring for a given string
The above is the detailed content of Python implements a function that accepts any number of parameters. For more information, please follow other related articles on the PHP Chinese website!

Pythonlistscanstoreanydatatype,arraymodulearraysstoreonetype,andNumPyarraysarefornumericalcomputations.1)Listsareversatilebutlessmemory-efficient.2)Arraymodulearraysarememory-efficientforhomogeneousdata.3)NumPyarraysareoptimizedforperformanceinscient

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

Pythonlistsarepartofthestandardlibrary,whilearraysarenot.Listsarebuilt-in,versatile,andusedforstoringcollections,whereasarraysareprovidedbythearraymoduleandlesscommonlyusedduetolimitedfunctionality.

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

Useanarray.arrayoveralistinPythonwhendealingwithhomogeneousdata,performance-criticalcode,orinterfacingwithCcode.1)HomogeneousData:Arrayssavememorywithtypedelements.2)Performance-CriticalCode:Arraysofferbetterperformancefornumericaloperations.3)Interf

No,notalllistoperationsaresupportedbyarrays,andviceversa.1)Arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,whichimpactsperformance.2)Listsdonotguaranteeconstanttimecomplexityfordirectaccesslikearraysdo.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
