Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

python - variable positional and printing the values

def minimum (*n):
        print(n)
minimum(1)
minimum(1,2)
    
    
def func(*args):
     print(args)
    
values1 = (1,2)
values2 = ((1,2), (3,4))
func(values1)
func(values2)

OUTPUT:
(1,)
(1, 2)
((1, 2),)
(((1, 2), (3, 4)),)

Process finished with exit code 0

First O/p: I think python is expecting multiple arguments to be passed so there is a comma (,) after 1. ?

Second O/p: Now the python sees multiple arguments being passed there is no comma. It stores the args a tuple?

Third O/p and Fourth O/p: Why is there still a comma? even after I passed 2 tuples assuming that python is expecting multiple tuples like the above?

Help me understand this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The first output shows a comma because without it, 1 being the only element, (1) would be just a integer (parentheses are wrapping the expression 1), (1,) is shown to differentiate tuples and simple parentheses.

in the second one, no trailing comma is needed to differentiate tuples, since there are more than one element.

In the third O/p, you are not passing 1 and 2, but instead you're passing the whole (1,2), so it shows only one item (which is (1,2)) in a tuple, and adds an extra comma. Same for the fourth: your passing the entire ((1,2), (3,4)).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...