Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: MRAB Newsgroups: comp.lang.python Subject: Re: one-element tuples [Was: Most probably a stupid question, but I still want to ask] Date: Mon, 11 Apr 2016 01:33:10 +0100 Lines: 56 Message-ID: References: <570AF0C6.7020007@mrabarnett.plus.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 5R0nItuclqXKMenfmx3rUgClyoDdJYK+b3wotA2fx0HA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'though:': 0.07; 'subject:question': 0.08; '(1,': 0.09; 'guys.': 0.09; 'subject:still': 0.09; 'tuple': 0.09; 'tuple.': 0.09; 'python': 0.10; ':-)': 0.12; 'exception': 0.13; 'subject: \n ': 0.15; '2))': 0.16; '2),': 0.16; 'already,': 0.16; 'arguments:': 0.16; 'comma': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'one-element': 0.16; 'parentheses': 0.16; 'received:192.168.1.4': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Was': 0.16; 'tuple,': 0.16; 'wrote:': 0.16; 'passes': 0.18; '(in': 0.18; '>>>': 0.20; 'pass': 0.22; 'trying': 0.22; 'passing': 0.23; 'this:': 0.23; 'second': 0.24; 'written': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; "doesn't": 0.26; 'separate': 0.27; 'question': 0.27; 'function': 0.28; 'subject: [': 0.29; 'convert': 0.29; 'guess': 0.31; 'int': 0.33; 'needed': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:192': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'behavior': 0.61; 'avoid': 0.61; 'making': 0.62; 'as:': 0.79; 'mandatory.': 0.84; 'observed': 0.84; 'piss': 0.84; 'subject:want': 0.93 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=K//fZHiI c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=IkcTkHD0fZMA:10 a=OnqwdpT3hySpQ5wv-t4A:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <570AF0C6.7020007@mrabarnett.plus.com> X-Mailman-Original-References: Xref: csiph.com comp.lang.python:106812 On 2016-04-11 01:13, Fillmore wrote: > > Sorry guys. It was not my intention to piss off anyone...just trying to understand how the languare works > > I guess that the answer to my question is: there is no such thing as a one-element tuple, > and Python will automatically convert a one-element tuple to a string... hence the > behavior I observed is explained... > > >>> a = ('hello','bonjour') > >>> b = ('hello') > >>> b > 'hello' > >>> a > ('hello', 'bonjour') > >>> > > > Did I get this right this time? > Nope. :-) A one-element tuple can be written as: >>> ('hello',) ('hello',) As has been said already, it's the comma that makes the tuple. The parentheses are often needed to avoid ambiguity. For example, object are passed into a function thus: f(x, y) (In reality, it's making a tuple and then passing that in.) What if you want to pass in the tuple (1, 2) as a single argument? f((1, 2)) If you write this: f(1, 2) it passes them in as 2 separate arguments. Or consider this: f((1, 2), 3) This has 2 arguments: the first is the tuple (1, 2) and the second is the int 3. There _is_ one exception though: (). It's the empty tuple (a 0-element tuple). It doesn't have a comma and the parentheses are mandatory. There's no other way to write it.