Parameter list with single argument

2024/10/8 10:58:44

When testing Python parameter list with a single argument, I found some weird behavior with print.

>>> def hi(*x):
...     print(x)
...
>>> hi()
()
>>> hi(1,2)
(1, 2)
>>> hi(1)
(1,)

Could any one explain to me what the last comma mean in hi(1)'s result (i.e. (1,))

Answer

Actually the behavior is only a little bit "weird." :-)

Your parameter x is prefixed with a star, which means all the arguments you pass to the function will be "rolled up" into a single tuple, and x will be that tuple.

The value (1,) is the way Python writes a tuple of one value, to contrast it with (1), which would be the number 1.

Here is a more general case:

def f(x, *y):return "x is {} and y is {}".format(x, y)

Here are some runs:

>>> f(1)
'x is 1 and y is ()'
>>> f(1, 2)
'x is 1 and y is (2,)'   
>>> f(1, 2, 3)
'x is 1 and y is (2, 3)'
>>> f(1, 2, 3, 4)
'x is 1 and y is (2, 3, 4)'

Notice how the first argument goes to x and all subsequent arguments are packed into the tuple y. You might just have found the way Python represents tuples with 0 or 1 components a little weird, but it makes sense when you realize that (1) has to be a number and so there has to be some way to represent a single-element tuple. Python just uses the trailing comma as a convention, that's all.

https://en.xdnf.cn/q/118707.html

Related Q&A

Scatter plot of values in pandas dataframe

I have a pandas dataframe in the following format. I am trying to plot this data based on ClusterAssigned, with probably different colors for 0 and 1. Distance ClusterAssigned23 135 120 …

String Delimiter in Python

I want to do split a string using "},{" as the delimiter. I have tried various things but none of them work.string="2,1,6,4,5,1},{8,1,4,9,6,6,7,0},{6,1,2,3,9},{2,3,5,4,3 "Split it i…

Wrong encoding of email attachment

I have a python 2.7 script running on windows. It logs in gmail, checks for new e-mails and attachments:#!/usr/bin/env python # -*- coding: utf-8 -*-file_types = ["pdf", "doc", &quo…

Blank lines in txt files in Python

I want to write sensor values to a text file with Python. All is working fine but one thing; in the text file, there are blank lines between each value. Its really annoying because I cant put the value…

Python 3 - decode spectroscopy data (Base64, IEEE754)

Im a chemist and working with spectroscopic data that was stored as a list (501 pairs of X,Y data) of Base64-encoded floating point values according to IEEE754.I tried to get an array of X, Y data to w…

Fill new column by following conditions listed in a dictionary [duplicate]

This question already has an answer here:Insert data to new column based on conditions given in dictionary(1 answer)Closed 2 years ago.I have the dictionary specifying the value the row should take if …

Pandas - Splitting dataframe into multiple excel workbooks by column value

Im new to pandas. I have a large excel file, what I’m trying to do is split the data frame after manipulation into multiple excel workbooks. There is more or less 400 vendors and I would like each To …

Create Sections in Python

I am newbie to Python. I have large file with repetitive string through the logsExample:abc def efg gjk abc def efg gjk abc def efg gjk abc def efg gjkExpected Result--------------------Section1-------…

Process CSV files in Python - Zero Imports/No Libraries

I have CSV example like this ID,TASK1,TASK2,QUIZ1,QUIZ2 11061,50,75,50,78 11062,70,80,60,50 11063,60,75,77,79 11064,52,85,50,80 11065,70,85,50,80how do i get the Max, Min and Avg on specific Column? i…

python - debugging: loop for plotting isnt showing the next plot

I need help in debugging. I just cant figure out why its not working as expected.The Code below should read data files (names are stored in all_files) in chunks of 6, arrange them in subplots (i,j indi…