Page 2 of 2

Re: Tuples

PostPosted: Mon Jun 24, 2013 4:27 pm
by Charles
That's right about Python not requiring parens. Though I wouldn't be opposed to requiring them outside of "return" and assignment. I'm trying to think of other places in Python where they are not required. If you want to pass them as arguments, they certainly need parens:
Code: Select all
# python
obj.foo((1, 2), (2, 1))  # 2 arguments, each a tuple of two ints
obj.foo(1, 2, 2, 1)  # 4 arguments, each an int


Btw the funny syntax that hopscc was thinking of comes into play when your tuple has only one element:
Code: Select all
# python
t = (1,)

Without a trailing comma, it would not be a tuple.

Re: Tuples

PostPosted: Tue Jun 25, 2013 5:33 am
by Chasm
Code: Select all
# python
t = (1,)

Without a trailing comma, it would not be a tuple.


That's really ugly

Re: Tuples

PostPosted: Tue Jun 25, 2013 7:03 am
by hopscc
(snap)
And also sorta confusing...
It may be a tuple if it has enclosing () but it doesnt always have to, sometime you need trailing commas but mostly not.
Its probably lexically convenient to tokenise/parse this way(in python) but its not very convenient for human recognition/learning/use.

I see Tuples as always useful for some things (multi-method returns with multi arg unpacking i.e ticket:315) and occasionally useful for some other things - like quick and dirty (temporary) immutable value class uses - you can do that with full blown classes but having Tuples available is faster, easier and more satisfying.

Re: Tuples

PostPosted: Tue Jun 25, 2013 2:20 pm
by Chasm
It's probably a stupid question, but couldn't tuples be something like:

variable = {'text', 14, 12.5f, DateTime.Now}
variable = function({'text', 14, 12.5f, DateTime.Now})


I'm really tired, so I'm not really thinking at this point, but it just seems like a neat use for curly braces (they need some love too), make it obvious that it's a tuple and support implicit tuples at the same time, no?

Just a thought I wanted to share before going to sleep. :D

Re: Tuples

PostPosted: Wed Jun 26, 2013 1:35 am
by hopscc
Cobra already uses {} for Set {} and Dict/Map {:} literals

oddLt10 = {1, 3, 5, 7, 9} # Set of Integers
mapto = {1:'Wun', 3:'free', 5:'Fyfe', 7:'sefun', 9:'nyne' } # map of int key to String value

inp = .gettaNumber
if inp in oddLt10
print 'Odd and less than 10 "[inp]"'
print 'input [inp] maps to', mapto[inp]

Re: Tuples

PostPosted: Wed Jun 26, 2013 2:36 am
by Chasm
Whoops, totally forgot. Sorry for that :oops:

Re: Tuples

PostPosted: Wed Jun 26, 2013 4:30 am
by hopscc
No prob - theres a lot in cobra to forget ;)