
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
why Python can't define tuples in a function?
Since Python 3.0, it is no longer possible to define unpacked tuple as a parameter in a function (PEP 3113). It means if you try to define a function as below −
def fn(a,(b,c)): pass
Python interpreter displays syntax error at first bracket of tuple. Instead, define tuple objects as parameter and unpack inside the function. In following code, two tuple objects representing x and y coordinates of two points are passed as parameters to calculate distance between the two. Before calculating, the tuple objects are unpacked in respective x and y coordinates.
def hypot(p1,p2): x1,y1=p1 x2,y2=p2 import math hyp=math.sqrt((x1-x2)**2+(y1-y2)**2) return hyp x=(10,10) y=(20,20) print ("hyp=",hypot(x,y))
- Related Articles
- Why can't we define a static method in a Java interface?
- Can we explicitly define datatype in a Python Function?
- How can we define a Python function at runtime?
- How to define a function in Python?
- Why can't my HTML file find the JavaScript function from a sourced module?
- Why plants can't talk?
- Why humans can't digest cellulose?
- Why can't we cook in glass containers?
- Why can't sand is soluble in water?
- Why a virus can't be killed using antibiotics?
- How we can iterate through a Python list of tuples?
- How can I subtract tuple of tuples from a tuple in Python?
- Why can't we live on Mars?
- Why can't we drink sea water?
- How can I preserve Python tuples with JSON?

Advertisements