For a while, i've been curious about a [Tuple Comprehension]
So finally i tried it, and the result was a bit surprising...
X= [ x for x in range(10) ]
X= ( x for x in range(10) )
print(X)
a= list(X)
print(a)
On 2/20/23 20:36, Hen Hanna wrote:
For a while, i've been curious about a [Tuple Comprehension]I've never heard of a "Tuple comprehension." No such thing exists as
far as I know.
So finally i tried it, and the result was a bit surprising...
X= [ x for x in range(10) ]
X= ( x for x in range(10) )
print(X)
a= list(X)
print(a)
What was surprising? Don't keep us in suspense!
Using square brackets is a list comprehension. Using parenthesis creates
a generator expression. It is not a tuple.
For a while, i've been curious about a [Tuple Comprehension]
So finally i tried it, and the result was a bit surprising...
X= [ x for x in range(10) ]
X= ( x for x in range(10) )
print(X)
a= list(X)
print(a)
For a while, i've been curious about a [Tuple Comprehension]
So finally i tried it, and the result was a bit surprising...
X= [ x for x in range(10) ]
X= ( x for x in range(10) )
(A) print( max( * LisX ))
(B) print( sum( * LisX )) <------- Bad syntax !!!
What's most surprising is.... (A) is ok, and (B) is not.
even tho' max() and sum() have (basically) the same syntax... ( takes one arg , whch is a list )
i've been programming for many years... ( just knew to Python )
(A) print( max( * LisX ))
(B) print( sum( * LisX )) <------- Bad syntax !!!
What's most surprising is.... (A) is ok, and (B) is not.
even tho' max() and sum() have (basically) the same syntax... ( takes one arg , whch is a list )
On 2/20/23 20:36, Hen Hanna wrote:
For a while, i've been curious about a [Tuple Comprehension]I've never heard of a "Tuple comprehension." No such thing exists as
far as I know.
So finally i tried it, and the result was a bit surprising...
X= [ x for x in range(10) ]
X= ( x for x in range(10) )
print(X)
a= list(X)
print(a)
What was surprising? Don't keep us in suspense!
Using square brackets is a list comprehension. Using parenthesis
creates a generator expression. It is not a tuple.
On 21/02/2023 04:13, Hen Hanna wrote:
(A) print( max( * LisX ))
(B) print( sum( * LisX )) <------- Bad syntax !!!
What's most surprising is.... (A) is ok, and (B) is not.
even tho' max() and sum() have (basically) the same syntax... ( takes one arg , whch is a list )
i've been programming for many years... ( just new to Python )
LOL, python is full of surprises. I'd definitely step into the same
piece of... Someday.
Of course 'Builtin functions' section explains that, but the
inconsistency is weird.
My response is absolutely useless, just two cents on the issue. Maybe
someone will fix that.
Axy.
On 21/02/2023 04:13, Hen Hanna wrote:
(A) print( max( * LisX ))
(B) print( sum( * LisX )) <------- Bad
syntax !!!
What's most surprising is.... (A) is ok, and (B) is not.
even tho' max() and sum() have (basically) the same
syntax... ( takes one arg , whch is a list )
Help on built-in function sum in module builtins:help(sum)
Help on built-in function max in module builtins:help(max)
i've been programming for many years... ( just knew to Python )
LOL, python is full of surprises. I'd definitely step into the same
piece of... Someday.
Of course 'Builtin functions' section explains that, but the
inconsistency is weird.
My response is absolutely useless, just two cents on the issue. Maybe
someone will fix that.
Axy.
(A) print( max( * LisX ))
(B) print( sum( * LisX )) <------- Bad syntax !!!
What's most surprising is.... (A) is ok, and (B) is not.
even tho' max() and sum() have (basically) the same syntax... ( takes one arg , whch is a list )
In your own code, you may want to either design your own functions, or use them as documented or perhaps create your own wrapper functions that carefully examine what you ask them to do and re-arrange as needed to call the function(s) you want asneeded or return their own values or better error messages. As a silly example, this fails:
max(1, "hello")case well.
Max expects all arguments to be of compatible types. You could write your own function called charMax() that converts all arguments to be of type str before calling max() or maybe call max(... , key=mycompare) where compare as a function handles this
The key point is that you need to adapt yourself to what some function you want to use offers, not expect the language to flip around at this point and start doing it your way and probably breaking many existing programs.
Yes, consistency is a good goal. Reality is a better goal.
disestablishmentarianism to measure length".split()words = "A sentence with sesquipedalian words like
...words
...max(words, key=len)
...def neglen(arg): return( - len(arg))
-5neglen("hello")
'A'max(words, key=neglen)
In your own code, you may want to either design your own functions, or usethem as documented or perhaps create your own wrapper functions that
max(1, "hello")own function called charMax() that converts all arguments to be of type str before calling max() or maybe call max(... , key=mycompare) where compare as
Max expects all arguments to be of compatible types. You could write your
The key point is that you need to adapt yourself to what some function youwant to use offers, not expect the language to flip around at this point and start doing it your way and probably breaking many existing programs.
Yes, consistency is a good goal. Reality is a better goal.
On 2/21/2023 12:32 PM, Axy via Python-list wrote:
On 21/02/2023 04:13, Hen Hanna wrote:They **don't** have basically the same signature, though. max() takes
(A) print( max( * LisX ))
(B) print( sum( * LisX )) <------- Bad
syntax !!!
What's most surprising is.... (A) is ok, and (B) is not.
even tho' max() and sum() have (basically) the same
syntax... ( takes one arg , whch is a list )
either an iterable or two or more numbers. Using max(*list_) presents
it with a series of numbers, so that's OK.
sum() takes just one iterable (plus an optional start index). Using sum(*list_) presents it with a series of numbers, and that does not
match its signature.
Check what I said:
Help on built-in function sum in module builtins:help(sum)
sum(iterable, /, start=0)
help(max)
On Tuesday, February 21, 2023 at 10:39:54 AM UTC-8, Thomas Passin wrote:
On 2/21/2023 12:32 PM, Axy via Python-list wrote:
On 21/02/2023 04:13, Hen Hanna wrote:They **don't** have basically the same signature, though. max() takes
(A) print( max( * LisX ))
(B) print( sum( * LisX )) <------- Bad
syntax !!!
What's most surprising is.... (A) is ok, and (B) is not.
even tho' max() and sum() have (basically) the same >>>> syntax... ( takes one arg , whch is a list )
either an iterable or two or more numbers. Using max(*list_) presents
it with a series of numbers, so that's OK.
sum() takes just one iterable (plus an optional start index). Using
sum(*list_) presents it with a series of numbers, and that does not
match its signature.
Check what I said:
Help on built-in function sum in module builtins:help(sum)
sum(iterable, /, start=0)
help(max)
thakns... i like the use of the word [signature]
thanks for all the commetns... i'll try to catch up later.
i think i understand it much better now.
regular Python (func-calling) notation is like CL (Common Lisp) funcall.
and fun( * args ) notation is like a (compile-time) macro
( max( * X )) ----macroexpand---> (apply max X)... and that one has to be an iterable.
( max( * [1,2,3,4] )) ----macroexpand---> (apply max '(1 2 3 4) )
and
Max() can take many arguments, but
Sum() can basically take only 1.
[0, 1, 2, 3, 4, 5, 6]list(range(7))
[11, 12, 13, 14, 15, 16, 17, 18, 19]list(range(11, 20))
[31, 34, 37, 40, 43]list(range(31, 45, 3))
Traceback (most recent call last):mylist = [31, 45, 3]
list(range(mylist))
[31, 34, 37, 40, 43]list(range(*mylist))
... print(f"alpha={alpha}, beta={beta}, gamma={gamma}")def myfunc(alpha=1, beta="", gamma=""):
alpha=1, beta=, gamma=myfunc()
alpha=1, beta=2, gamma=3myfunc(1, 2, 3)
{'alpha': 101, 'beta': 'hello', 'gamma': 'buy bye'}mydict = { "alpha" : 101, "beta" : "hello", "gamma" : "buy bye" }
mydict
alpha=101, beta=hello, gamma=buy byemyfunc( **mydict )
Traceback (most recent call last):sum([1, 2], [3,4])
10sum([1, 2] + [3,4])
...def multiSum(*args): return sum(list(args))
15multiSum(1, 2, 3, 4 , 5)
...def multiSum(*args): return sum(args)
15multiSum(1, 2, 3, 4 , 5)
On 2/21/2023 12:32 PM, Axy via Python-list wrote:
On 21/02/2023 04:13, Hen Hanna wrote:They **don't** have basically the same signature, though. max() takes
(A) print( max( * LisX ))
(B) print( sum( * LisX )) <------- Bad
syntax !!!
What's most surprising is.... (A) is ok, and (B) is not.
even tho' max() and sum() have (basically) the same
syntax... ( takes one arg , whch is a list )
either an iterable or two or more numbers. Using max(*list_) presents
it with a series of numbers, so that's OK.
sum() takes just one iterable (plus an optional start index). Using sum(*list_) presents it with a series of numbers, and that does not
match its signature.
Check what I said:
Help on built-in function sum in module builtins:help(sum)
sum(iterable, /, start=0)
help(max)
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (0 / 16) |
Uptime: | 166:07:22 |
Calls: | 10,385 |
Calls today: | 2 |
Files: | 14,057 |
Messages: | 6,416,528 |