SlideShare a Scribd company logo
amitu.com
Function arguments:
positional and keyword
Excerpt from Python For Programmers
amitu.com Descriptors In Python
This is from a section in my “Python for
Developers” book. The book covers python
material for people who are already
developers, even python developers.
Check it out: amitu.com/python/
amitu.com Descriptors In Python
In this we are going to learn about function
arguments in Python
Positional and Keyword arguments, *
and ** syntax etc.
amitu.com Function arguments: positional and keyword
Functions can take arguments when called.
Here we have defined a function foo, that takes
a single “positional argument”, x.
When we called foo(12), 12 got assigned to x
when the control was inside foo().
amitu.com Function arguments: positional and keyword
We can have more than one arguments:
So far so good.
amitu.com Function arguments: positional and keyword
One problem with function taking many arguments
is remembering the order of arguments.
amitu.com Function arguments: positional and keyword
Knowing that a function add_user() exists is not
enough, nor is knowing that it takes name, gender
and location. You must also remember in what order
those three must be supplied.
How do we make such obviously wrong code
look wrong?
amitu.com Function arguments: positional and keyword
Sometimes your code editor can help you. But we
can not always rely on that. What if you are
browsing code on GitHub?
amitu.com Function arguments: positional and keyword
To solve this precise problem, Python
supports function calling using what is called
“keyword argument” syntax:
… as against “positional arguments”, eg
foo(10, 20), where arguments are identified by
their position.
amitu.com Function arguments: positional and keyword
When using keyword arguments, you can even
change the order of keyword arguments, and
python will do the right thing.
amitu.com Function arguments: positional and keyword
We can also mix both positional and
keyword arguments:
isn't Python cool?
amitu.com Function arguments: positional and keyword
There is one constraint—we can not pass a
positional argument after a keyword argument. If we
use both, all positional arguments must come before
any keyword argument.
amitu.com Function arguments: positional and keyword
Default Values
Next:
amitu.com Function arguments: positional and keyword
In Python, we can define functions with
arguments that take default values.
amitu.com Function arguments: positional and keyword
We defined foo() to have default value of
10 for y. If we don't pass anything for y,
python uses the default value.
amitu.com Function arguments: positional and keyword
Only y has a default value, x is still a
required argument.
amitu.com Function arguments: positional and keyword
We can still use keyword arguments:
Or a mixed arguments:
amitu.com Function arguments: positional and keyword
NOTE: like we can not pass keyword arguments after
positional arguments when calling function, we
similarly cannot define functions with default values
before arguments that do not have default values.
Arguments with default values must come at the end.
amitu.com Function arguments: positional and keyword
Mutable default value
gotcha
amitu.com Function arguments: positional and keyword
One common gotcha when using default arguments is
using a mutable type as the default value.
Default value for users here is a list, [ ], which is a
mutable data type in Python.
amitu.com Function arguments: positional and keyword
Let us say our intention is to create a users list. If it is
not passed we want to use an empty list by default. We
append to passed list, or the default empty list, and
returned the modified list.
amitu.com Function arguments: positional and keyword
So far, so good, and it seems to be working fine.
Let us see when and where the problem begins.
What happened here? Why is “sam” in the
free_users list? add_user() should have taken
default empty list, and added “richard” and
returned that. Why two members?
amitu.com Function arguments: positional and keyword
What is happening is that, the empty list we have used
as default value of users in the function definition…
… the same instance is used as value of users,
every time we do not pass the second parameter.
And by the second time function is called,
the list is not empty any more!
amitu.com Function arguments: positional and keyword
The proper way to write such a function would
be using a value that would never make sense
to be passed, say None or -1.
ASIDE: such use of a value that is used for a
special purpose is called a sentinel value.
amitu.com Function arguments: positional and keyword
There is another subtle mistake we can make.
Let us look at another version of add_user() to
give emphasis to it.
Here, we first check if the passed in value is None.
We have used strong identity check (is None).
amitu.com Function arguments: positional and keyword
The difference is subtle users is None vs not users.
First creates a new list if nothing was passed (and we took
the default value of None), other if empty list was passed.
amitu.com Function arguments: positional and keyword
Yet another more reliable way to create
sentinel values is:
amitu.com Function arguments: positional and keyword
The reason such NotPassed is preferable over None
is because our code can have None assigned to
something by mistake, but getting NotPassed
assigned to some variable has to be intentional.
amitu.com Function arguments: positional and keyword
Variable Arguments
Sometimes we want to write functions that
take variable number of arguments.
amitu.com Function arguments: positional and keyword
This is how we write a function in Python that
takes variable number of arguments:
amitu.com Function arguments: positional and keyword
Note the *args. The * indicates to Python
that this function takes the variable number
of positional arguments, and when the
function is called, all those arguments are
stored in tuple() and passed to the function
by the name args. Instead of args, we could
have called it anything.
amitu.com Function arguments: positional and keyword
We can not treat args as a keyword argument.
But we can achieve the same by * syntax:
amitu.com Function arguments: positional and keyword
We have used * while calling the function. When
using this syntax, we must pass an iterable. It
can be a tuple, a list, or any iterable.
Note that the list was converted to a tuple.
Whatever you pass will get converted to tuple.
amitu.com Function arguments: positional and keyword
We can use this syntax even for functions that do
not take a variable number of arguments…
amitu.com Function arguments: positional and keyword
… as long as the number of items we pass is correct.
amitu.com Function arguments: positional and keyword
You can also mix variable arguments with
positional arguments.
amitu.com Function arguments: positional and keyword
Remember that we can not pass positional
arguments after keyword arguments.
amitu.com Function arguments: positional and keyword
In Python 2, this would not be valid:
amitu.com Function arguments: positional and keyword
Python 3 accepts it, but treats y as a compulsory
keyword argument.
amitu.com Function arguments: positional and keyword
A variable number of arguments works with default
value arguments too.
… both in Python 2 and Python 3.
amitu.com Function arguments: positional and keyword
But there is a possibility of ambiguity here.
Python would not accept it.
amitu.com Function arguments: positional and keyword
Like we have *arg syntax which tells Python to
capture all remaining positional arguments,
there is a similar **kw syntax that tells Python
to capture all remaining keyword arguments
even ones we never defined.
amitu.com Function arguments: positional and keyword
Python will capture all the keyword
arguments in a dict(), so everything that we
know about dict() can be used with them.
amitu.com Function arguments: positional and keyword
A new dictionary is constructed for you every
time a function that takes variable number of
keyword arguments is called.
amitu.com Function arguments: positional and keyword
In this case, our function accepts no
positional argument, so we can not call it
with a positional argument.
amitu.com Function arguments: positional and keyword
You can mix and match all forms of
arguments in a single function.
Just be careful that keyword arguments must come after
positional ones, and there must not be any ambiguities.
amitu.com Function arguments: positional and keyword
Finally, you can use the **{} syntax when
calling a function too.
Even when the function being called doesn't
take keyword arguments…
amitu.com Function arguments: positional and keyword
With these features, we can create a "universal
function" that can be called with any combination of
positional and keyword arguments:
These play special role when we are writing generic
decorators, or when we want to override a method
without knowing the parent methods signature.
amitu.com Descriptors In Python
Thats it for now!
You have seen a section in my “Python for
Developers” book. The book covers
python material for people who're already
developers, even python developers.
Check it out: amitu.com/python/

More Related Content

What's hot (20)

Python Dictionary.pptx
Python Dictionary.pptxPython Dictionary.pptx
Python Dictionary.pptx
Sanad Bhowmik
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
Mohammed Sikander
 
Python Pandas
Python PandasPython Pandas
Python Pandas
Sunil OS
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
What is Tuple in python? | Python Tuple Tutorial | Edureka
What is Tuple in python? | Python Tuple Tutorial | EdurekaWhat is Tuple in python? | Python Tuple Tutorial | Edureka
What is Tuple in python? | Python Tuple Tutorial | Edureka
Edureka!
 
Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...
Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...
Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...
Edureka!
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
hydpy
 
Data types in python
Data types in pythonData types in python
Data types in python
RaginiJain21
 
Python ppt
Python pptPython ppt
Python ppt
Mohita Pandey
 
Introduction to matplotlib
Introduction to matplotlibIntroduction to matplotlib
Introduction to matplotlib
Piyush rai
 
Python If Else | If Else Statement In Python | Edureka
Python If Else | If Else Statement In Python | EdurekaPython If Else | If Else Statement In Python | Edureka
Python If Else | If Else Statement In Python | Edureka
Edureka!
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
Devashish Kumar
 
Python Dictionaries and Sets
Python Dictionaries and SetsPython Dictionaries and Sets
Python Dictionaries and Sets
Nicole Ryan
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
moazamali28
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
 
Python-Inheritance.pptx
Python-Inheritance.pptxPython-Inheritance.pptx
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
Full Python in 20 slides
Full Python in 20 slidesFull Python in 20 slides
Full Python in 20 slides
rfojdar
 
2D Array
2D Array 2D Array
2D Array
Ehatsham Riaz
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
eShikshak
 
Python Dictionary.pptx
Python Dictionary.pptxPython Dictionary.pptx
Python Dictionary.pptx
Sanad Bhowmik
 
Python Pandas
Python PandasPython Pandas
Python Pandas
Sunil OS
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
What is Tuple in python? | Python Tuple Tutorial | Edureka
What is Tuple in python? | Python Tuple Tutorial | EdurekaWhat is Tuple in python? | Python Tuple Tutorial | Edureka
What is Tuple in python? | Python Tuple Tutorial | Edureka
Edureka!
 
Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...
Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...
Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...
Edureka!
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
hydpy
 
Data types in python
Data types in pythonData types in python
Data types in python
RaginiJain21
 
Introduction to matplotlib
Introduction to matplotlibIntroduction to matplotlib
Introduction to matplotlib
Piyush rai
 
Python If Else | If Else Statement In Python | Edureka
Python If Else | If Else Statement In Python | EdurekaPython If Else | If Else Statement In Python | Edureka
Python If Else | If Else Statement In Python | Edureka
Edureka!
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
Devashish Kumar
 
Python Dictionaries and Sets
Python Dictionaries and SetsPython Dictionaries and Sets
Python Dictionaries and Sets
Nicole Ryan
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
moazamali28
 
Full Python in 20 slides
Full Python in 20 slidesFull Python in 20 slides
Full Python in 20 slides
rfojdar
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
eShikshak
 

Viewers also liked (20)

python Function
python Function python Function
python Function
Ronak Rathi
 
Coverfox: Tech Productivity etc
Coverfox: Tech Productivity etcCoverfox: Tech Productivity etc
Coverfox: Tech Productivity etc
Amit Upadhyay
 
Functions in python
Functions in python Functions in python
Functions in python
baabtra.com - No. 1 supplier of quality freshers
 
Descriptors In Python
Descriptors In PythonDescriptors In Python
Descriptors In Python
Amit Upadhyay
 
Day2
Day2Day2
Day2
Karin Lagesen
 
Functions
FunctionsFunctions
Functions
Marieswaran Ramasamy
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)
Rick Copeland
 
Input and Output
Input and OutputInput and Output
Input and Output
Marieswaran Ramasamy
 
Lazy evaluation in Python
Lazy evaluation in PythonLazy evaluation in Python
Lazy evaluation in Python
Rahul Pydimukkala
 
Python testing-frameworks overview
Python testing-frameworks overviewPython testing-frameworks overview
Python testing-frameworks overview
Jachym Cepicky
 
Python datatype
Python datatypePython datatype
Python datatype
건희 김
 
4. python functions
4. python   functions4. python   functions
4. python functions
in4400
 
Python Modules
Python ModulesPython Modules
Python Modules
Nitin Reddy Katkam
 
Functions in python
Functions in pythonFunctions in python
Functions in python
Ilian Iliev
 
Taking care of your computers
Taking care of your computersTaking care of your computers
Taking care of your computers
Jean Ulpindo
 
Python for All
Python for All Python for All
Python for All
Pragya Goyal
 
Ansible loves Python, Python Philadelphia meetup
Ansible loves Python, Python Philadelphia meetupAnsible loves Python, Python Philadelphia meetup
Ansible loves Python, Python Philadelphia meetup
Greg DeKoenigsberg
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
Sujith Kumar
 
Learning python with flask (PyLadies Malaysia 2017 Workshop #1)
Learning python with flask (PyLadies Malaysia 2017 Workshop #1)Learning python with flask (PyLadies Malaysia 2017 Workshop #1)
Learning python with flask (PyLadies Malaysia 2017 Workshop #1)
Sian Lerk Lau
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
Sujith Kumar
 
python Function
python Function python Function
python Function
Ronak Rathi
 
Coverfox: Tech Productivity etc
Coverfox: Tech Productivity etcCoverfox: Tech Productivity etc
Coverfox: Tech Productivity etc
Amit Upadhyay
 
Descriptors In Python
Descriptors In PythonDescriptors In Python
Descriptors In Python
Amit Upadhyay
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)
Rick Copeland
 
Python testing-frameworks overview
Python testing-frameworks overviewPython testing-frameworks overview
Python testing-frameworks overview
Jachym Cepicky
 
Python datatype
Python datatypePython datatype
Python datatype
건희 김
 
4. python functions
4. python   functions4. python   functions
4. python functions
in4400
 
Functions in python
Functions in pythonFunctions in python
Functions in python
Ilian Iliev
 
Taking care of your computers
Taking care of your computersTaking care of your computers
Taking care of your computers
Jean Ulpindo
 
Ansible loves Python, Python Philadelphia meetup
Ansible loves Python, Python Philadelphia meetupAnsible loves Python, Python Philadelphia meetup
Ansible loves Python, Python Philadelphia meetup
Greg DeKoenigsberg
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
Sujith Kumar
 
Learning python with flask (PyLadies Malaysia 2017 Workshop #1)
Learning python with flask (PyLadies Malaysia 2017 Workshop #1)Learning python with flask (PyLadies Malaysia 2017 Workshop #1)
Learning python with flask (PyLadies Malaysia 2017 Workshop #1)
Sian Lerk Lau
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Ad

Similar to Function arguments In Python (20)

Functions in Python with all type of arguments
Functions in Python with all type of argumentsFunctions in Python with all type of arguments
Functions in Python with all type of arguments
riazahamed37
 
Python Programming - Functions and Modules
Python Programming - Functions and ModulesPython Programming - Functions and Modules
Python Programming - Functions and Modules
Omid AmirGhiasvand
 
beginners_python_cheat_sheet_pcc_functions.pdf
beginners_python_cheat_sheet_pcc_functions.pdfbeginners_python_cheat_sheet_pcc_functions.pdf
beginners_python_cheat_sheet_pcc_functions.pdf
GuarachandarChand
 
functionnotes.pdf
functionnotes.pdffunctionnotes.pdf
functionnotes.pdf
AXL Computer Academy
 
Function in Python
Function in PythonFunction in Python
Function in Python
Yashdev Hada
 
Lecture 08.pptx
Lecture 08.pptxLecture 08.pptx
Lecture 08.pptx
Mohammad Hassan
 
Functions2.pptx
Functions2.pptxFunctions2.pptx
Functions2.pptx
AkhilTyagi42
 
_Python_ Functions _and_ Libraries_.pptx
_Python_ Functions _and_ Libraries_.pptx_Python_ Functions _and_ Libraries_.pptx
_Python_ Functions _and_ Libraries_.pptx
yaramahsoob
 
Functionscs12 ppt.pdf
Functionscs12 ppt.pdfFunctionscs12 ppt.pdf
Functionscs12 ppt.pdf
RiteshKumarPradhan1
 
Functions.pdf
Functions.pdfFunctions.pdf
Functions.pdf
kailashGusain3
 
Functions_21_22.pdf
Functions_21_22.pdfFunctions_21_22.pdf
Functions_21_22.pdf
paijitk
 
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfUserdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdf
DeeptiMalhotra19
 
functions _
functions                                 _functions                                 _
functions _
SwatiHans10
 
Function in Python function in python.pptx
Function in Python function in python.pptxFunction in Python function in python.pptx
Function in Python function in python.pptx
JHILIPASAYAT
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
Daddy84
 
UNIT 3 python.pptx
UNIT 3 python.pptxUNIT 3 python.pptx
UNIT 3 python.pptx
TKSanthoshRao
 
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
Osama Ghandour Geris
 
Chapter 2 Python Functions
Chapter 2               Python FunctionsChapter 2               Python Functions
Chapter 2 Python Functions
11210208
 
Unit_2.0_Functions (1).pdfUnit_2.0_Functions (1).pdf
Unit_2.0_Functions (1).pdfUnit_2.0_Functions (1).pdfUnit_2.0_Functions (1).pdfUnit_2.0_Functions (1).pdf
Unit_2.0_Functions (1).pdfUnit_2.0_Functions (1).pdf
RutviBaraiya
 
Powerpoint presentation for Python Functions
Powerpoint presentation for Python FunctionsPowerpoint presentation for Python Functions
Powerpoint presentation for Python Functions
BalaSubramanian376976
 
Functions in Python with all type of arguments
Functions in Python with all type of argumentsFunctions in Python with all type of arguments
Functions in Python with all type of arguments
riazahamed37
 
Python Programming - Functions and Modules
Python Programming - Functions and ModulesPython Programming - Functions and Modules
Python Programming - Functions and Modules
Omid AmirGhiasvand
 
beginners_python_cheat_sheet_pcc_functions.pdf
beginners_python_cheat_sheet_pcc_functions.pdfbeginners_python_cheat_sheet_pcc_functions.pdf
beginners_python_cheat_sheet_pcc_functions.pdf
GuarachandarChand
 
Function in Python
Function in PythonFunction in Python
Function in Python
Yashdev Hada
 
_Python_ Functions _and_ Libraries_.pptx
_Python_ Functions _and_ Libraries_.pptx_Python_ Functions _and_ Libraries_.pptx
_Python_ Functions _and_ Libraries_.pptx
yaramahsoob
 
Functions_21_22.pdf
Functions_21_22.pdfFunctions_21_22.pdf
Functions_21_22.pdf
paijitk
 
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfUserdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdf
DeeptiMalhotra19
 
Function in Python function in python.pptx
Function in Python function in python.pptxFunction in Python function in python.pptx
Function in Python function in python.pptx
JHILIPASAYAT
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
Daddy84
 
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
Osama Ghandour Geris
 
Chapter 2 Python Functions
Chapter 2               Python FunctionsChapter 2               Python Functions
Chapter 2 Python Functions
11210208
 
Unit_2.0_Functions (1).pdfUnit_2.0_Functions (1).pdf
Unit_2.0_Functions (1).pdfUnit_2.0_Functions (1).pdfUnit_2.0_Functions (1).pdfUnit_2.0_Functions (1).pdf
Unit_2.0_Functions (1).pdfUnit_2.0_Functions (1).pdf
RutviBaraiya
 
Powerpoint presentation for Python Functions
Powerpoint presentation for Python FunctionsPowerpoint presentation for Python Functions
Powerpoint presentation for Python Functions
BalaSubramanian376976
 
Ad

Recently uploaded (20)

ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
Soulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate reviewSoulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate review
Soulmaite
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyesEnd-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
ThousandEyes
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Dancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptxDancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptx
Elliott Richmond
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
Introduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUEIntroduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUE
Google Developer Group On Campus European Universities in Egypt
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
Edge AI and Vision Alliance
 
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
Soulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate reviewSoulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate review
Soulmaite
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyesEnd-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
ThousandEyes
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Dancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptxDancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptx
Elliott Richmond
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
Edge AI and Vision Alliance
 

Function arguments In Python

  • 1. amitu.com Function arguments: positional and keyword Excerpt from Python For Programmers
  • 2. amitu.com Descriptors In Python This is from a section in my “Python for Developers” book. The book covers python material for people who are already developers, even python developers. Check it out: amitu.com/python/
  • 3. amitu.com Descriptors In Python In this we are going to learn about function arguments in Python Positional and Keyword arguments, * and ** syntax etc.
  • 4. amitu.com Function arguments: positional and keyword Functions can take arguments when called. Here we have defined a function foo, that takes a single “positional argument”, x. When we called foo(12), 12 got assigned to x when the control was inside foo().
  • 5. amitu.com Function arguments: positional and keyword We can have more than one arguments: So far so good.
  • 6. amitu.com Function arguments: positional and keyword One problem with function taking many arguments is remembering the order of arguments.
  • 7. amitu.com Function arguments: positional and keyword Knowing that a function add_user() exists is not enough, nor is knowing that it takes name, gender and location. You must also remember in what order those three must be supplied. How do we make such obviously wrong code look wrong?
  • 8. amitu.com Function arguments: positional and keyword Sometimes your code editor can help you. But we can not always rely on that. What if you are browsing code on GitHub?
  • 9. amitu.com Function arguments: positional and keyword To solve this precise problem, Python supports function calling using what is called “keyword argument” syntax: … as against “positional arguments”, eg foo(10, 20), where arguments are identified by their position.
  • 10. amitu.com Function arguments: positional and keyword When using keyword arguments, you can even change the order of keyword arguments, and python will do the right thing.
  • 11. amitu.com Function arguments: positional and keyword We can also mix both positional and keyword arguments: isn't Python cool?
  • 12. amitu.com Function arguments: positional and keyword There is one constraint—we can not pass a positional argument after a keyword argument. If we use both, all positional arguments must come before any keyword argument.
  • 13. amitu.com Function arguments: positional and keyword Default Values Next:
  • 14. amitu.com Function arguments: positional and keyword In Python, we can define functions with arguments that take default values.
  • 15. amitu.com Function arguments: positional and keyword We defined foo() to have default value of 10 for y. If we don't pass anything for y, python uses the default value.
  • 16. amitu.com Function arguments: positional and keyword Only y has a default value, x is still a required argument.
  • 17. amitu.com Function arguments: positional and keyword We can still use keyword arguments: Or a mixed arguments:
  • 18. amitu.com Function arguments: positional and keyword NOTE: like we can not pass keyword arguments after positional arguments when calling function, we similarly cannot define functions with default values before arguments that do not have default values. Arguments with default values must come at the end.
  • 19. amitu.com Function arguments: positional and keyword Mutable default value gotcha
  • 20. amitu.com Function arguments: positional and keyword One common gotcha when using default arguments is using a mutable type as the default value. Default value for users here is a list, [ ], which is a mutable data type in Python.
  • 21. amitu.com Function arguments: positional and keyword Let us say our intention is to create a users list. If it is not passed we want to use an empty list by default. We append to passed list, or the default empty list, and returned the modified list.
  • 22. amitu.com Function arguments: positional and keyword So far, so good, and it seems to be working fine. Let us see when and where the problem begins. What happened here? Why is “sam” in the free_users list? add_user() should have taken default empty list, and added “richard” and returned that. Why two members?
  • 23. amitu.com Function arguments: positional and keyword What is happening is that, the empty list we have used as default value of users in the function definition… … the same instance is used as value of users, every time we do not pass the second parameter. And by the second time function is called, the list is not empty any more!
  • 24. amitu.com Function arguments: positional and keyword The proper way to write such a function would be using a value that would never make sense to be passed, say None or -1. ASIDE: such use of a value that is used for a special purpose is called a sentinel value.
  • 25. amitu.com Function arguments: positional and keyword There is another subtle mistake we can make. Let us look at another version of add_user() to give emphasis to it. Here, we first check if the passed in value is None. We have used strong identity check (is None).
  • 26. amitu.com Function arguments: positional and keyword The difference is subtle users is None vs not users. First creates a new list if nothing was passed (and we took the default value of None), other if empty list was passed.
  • 27. amitu.com Function arguments: positional and keyword Yet another more reliable way to create sentinel values is:
  • 28. amitu.com Function arguments: positional and keyword The reason such NotPassed is preferable over None is because our code can have None assigned to something by mistake, but getting NotPassed assigned to some variable has to be intentional.
  • 29. amitu.com Function arguments: positional and keyword Variable Arguments Sometimes we want to write functions that take variable number of arguments.
  • 30. amitu.com Function arguments: positional and keyword This is how we write a function in Python that takes variable number of arguments:
  • 31. amitu.com Function arguments: positional and keyword Note the *args. The * indicates to Python that this function takes the variable number of positional arguments, and when the function is called, all those arguments are stored in tuple() and passed to the function by the name args. Instead of args, we could have called it anything.
  • 32. amitu.com Function arguments: positional and keyword We can not treat args as a keyword argument. But we can achieve the same by * syntax:
  • 33. amitu.com Function arguments: positional and keyword We have used * while calling the function. When using this syntax, we must pass an iterable. It can be a tuple, a list, or any iterable. Note that the list was converted to a tuple. Whatever you pass will get converted to tuple.
  • 34. amitu.com Function arguments: positional and keyword We can use this syntax even for functions that do not take a variable number of arguments…
  • 35. amitu.com Function arguments: positional and keyword … as long as the number of items we pass is correct.
  • 36. amitu.com Function arguments: positional and keyword You can also mix variable arguments with positional arguments.
  • 37. amitu.com Function arguments: positional and keyword Remember that we can not pass positional arguments after keyword arguments.
  • 38. amitu.com Function arguments: positional and keyword In Python 2, this would not be valid:
  • 39. amitu.com Function arguments: positional and keyword Python 3 accepts it, but treats y as a compulsory keyword argument.
  • 40. amitu.com Function arguments: positional and keyword A variable number of arguments works with default value arguments too. … both in Python 2 and Python 3.
  • 41. amitu.com Function arguments: positional and keyword But there is a possibility of ambiguity here. Python would not accept it.
  • 42. amitu.com Function arguments: positional and keyword Like we have *arg syntax which tells Python to capture all remaining positional arguments, there is a similar **kw syntax that tells Python to capture all remaining keyword arguments even ones we never defined.
  • 43. amitu.com Function arguments: positional and keyword Python will capture all the keyword arguments in a dict(), so everything that we know about dict() can be used with them.
  • 44. amitu.com Function arguments: positional and keyword A new dictionary is constructed for you every time a function that takes variable number of keyword arguments is called.
  • 45. amitu.com Function arguments: positional and keyword In this case, our function accepts no positional argument, so we can not call it with a positional argument.
  • 46. amitu.com Function arguments: positional and keyword You can mix and match all forms of arguments in a single function. Just be careful that keyword arguments must come after positional ones, and there must not be any ambiguities.
  • 47. amitu.com Function arguments: positional and keyword Finally, you can use the **{} syntax when calling a function too. Even when the function being called doesn't take keyword arguments…
  • 48. amitu.com Function arguments: positional and keyword With these features, we can create a "universal function" that can be called with any combination of positional and keyword arguments: These play special role when we are writing generic decorators, or when we want to override a method without knowing the parent methods signature.
  • 49. amitu.com Descriptors In Python Thats it for now! You have seen a section in my “Python for Developers” book. The book covers python material for people who're already developers, even python developers. Check it out: amitu.com/python/