473,324 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Help me understand this

Hi,

Pls tell me whats going on in the code snippet below:
>>n = 10
statstr = "N = ",n
type(statstr) #case1
<type 'tuple'>
>>print statstr
('N = ', 10)
>>print "N = ",n #case 2
N = 10

In the first case the result is printed as a tuple and in the second
it appears as an ordinary string.

Jan 30 '07 #1
8 1127
On Mon, 29 Jan 2007 23:05:37 -0800, ArdPy wrote:
Hi,

Pls tell me whats going on in the code snippet below:
>>>n = 10
statstr = "N = ",n
type(statstr) #case1
<type 'tuple'>
>>>print statstr
('N = ', 10)
>>>print "N = ",n #case 2
N = 10

In the first case the result is printed as a tuple and in the second
it appears as an ordinary string.

The print statement takes one or more comma-delimited objects, and prints
each one separated with a space, and then finally prints a newline. If you
end the list with a comma, no newline is printed. So when you execute the
line:

print "N = ", n

Python prints "N = ", then a space, then 10, then a newline.
Outside of a print statement (and also an "except" statement), commas
create tuples. So when you execute the line:

statstr = "N = ", n

the right-hand side is a tuple with two items. The first item is the
string "N = " and the second item is the integer currently named n (in
your case, 10).

Do not be fooled that brackets make tuples -- they don't. With the
exception of the empty tuple () which is a special case, it is commas that
make tuples. So these two lines have identical effects:

t = 1, 2, 3
t = (1, 2, 3)

That is why these two statements print something different:

print 1,2,3
print (1,2,3)

In the second case, the brackets force the expression "1,2,3" to be
evaluated before the print statement sees it, so the print statement sees
a single argument which is a tuple, instead of three int arguments.

(But notice that the brackets still aren't creating the tuple, the commas
are. The brackets just change the order of evaluation.)

--
Steven D'Aprano

Jan 30 '07 #2
On Jan 29, 11:47 pm, Steven D'Aprano
<s...@REMOVEME.cybersource.com.auwrote:
Outside of a print statement (and also an "except" statement), commas
create tuples.
And function calls:
>>3,
(3,)
>>type(3,)
<type 'int'>
>>type((3,))
<type 'tuple'>

But here's one I still don't get:
>>type(2)
<type 'int'>
>>type((2))
<type 'int'>
>>(2).__add__(1)
3
>>2.__add__(1)
File "<stdin>", line 1
2.__add__(1)
^
SyntaxError: invalid syntax

-Beej

Jan 30 '07 #3
Beej wrote:
On Jan 29, 11:47 pm, Steven D'Aprano
<s...@REMOVEME.cybersource.com.auwrote:
>Outside of a print statement (and also an "except" statement), commas
create tuples.

And function calls:
>>>3,
(3,)
>>>type(3,)
<type 'int'>
>>>type((3,))
<type 'tuple'>

But here's one I still don't get:
>>>type(2)
<type 'int'>
>>>type((2))
<type 'int'>
>>>(2).__add__(1)
3
>>>2.__add__(1)
File "<stdin>", line 1
2.__add__(1)
^
SyntaxError: invalid syntax
Because 2. is the start of a float-literal. That isn't distinguishable for
the parsere otherwise.
Diez
Jan 30 '07 #4
Beej wrote:
>>>(2).__add__(1)
Nice. I would have never thought to put parentheses around an integer to
get at its attributes.

James
Jan 30 '07 #5
En Tue, 30 Jan 2007 06:34:01 -0300, Beej <be**@beej.usescribió:
But here's one I still don't get:
>>>type(2)
<type 'int'>
>>>type((2))
<type 'int'>
>>>(2).__add__(1)
3
>>>2.__add__(1)
File "<stdin>", line 1
2.__add__(1)
^
SyntaxError: invalid syntax
It appears to be a bug, either in the grammar implementation, or in the
grammar documentation.
These are the relevant rules:

attributeref ::= primary "." identifier

primary ::= atom | attributeref | subscription | slicing | call

atom ::= identifier | literal | enclosure

literal ::= stringliteral | integer | longinteger | floatnumber |
imagnumber

An integer is a primary so 2.__add(1) should be valid.

--
Gabriel Genellina

Jan 30 '07 #6
On 2007-01-30, Gabriel Genellina <ga******@yahoo.com.arwrote:
En Tue, 30 Jan 2007 06:34:01 -0300, Beej <be**@beej.usescribió:
>But here's one I still don't get:
>>>>type(2)
<type 'int'>
>>>>type((2))
<type 'int'>
>>>>(2).__add__(1)
3
>>>>2.__add__(1)
File "<stdin>", line 1
2.__add__(1)
^
SyntaxError: invalid syntax

It appears to be a bug, either in the grammar implementation, or in the
grammar documentation.
These are the relevant rules:

attributeref ::= primary "." identifier

primary ::= atom | attributeref | subscription | slicing | call

atom ::= identifier | literal | enclosure

literal ::= stringliteral | integer | longinteger | floatnumber |
imagnumber

An integer is a primary so 2.__add(1) should be valid.
Not if the tokenizer passes the parser a float.

--
Neil Cerutti
Jan 30 '07 #7
On Jan 30, 1:38 am, "Diez B. Roggisch" <d...@nospam.web.dewrote:
Because 2. is the start of a float-literal. That isn't distinguishable for
the parsere otherwise.
Oh, excellent! I wonder why I didn't think of that--I was too busy in
"get a field" mode it didn't even occur to me that the "." had a
different context, no matter how much more obvious.
>>print 2.
2.0
>>type(2.)
<type 'float'>

-Beej

Jan 30 '07 #8
James Stroud a écrit :
Beej wrote:
>>>>(2).__add__(1)

Nice. I would have never thought to put parentheses around an integer to
get at its attributes.

James
You can also do it like that :
>>2 .__add__(1)
3
Jan 31 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: rami | last post by:
I have some code which does following thing template<class X, unsigned ID = 0> struct SomeStruct { template<class X> static SomeStruct<X, ID + 1>& SomeFunc(); ... ... ... ...
9
by: Daz | last post by:
Hello hello! I'm trying to finish off putting my design into HTML and I've come across a problem that I can't get my head around. I've got divs floating in two columns, but I'm having problems...
3
by: HLong | last post by:
I am trying to understand an example where the Least Significant Bit is replaced. The code is in C# and I am having problems with a line that reads: B= (byte) (Value == 1 ? B | (1 << poss) : B & ~...
0
by: J. | last post by:
Hello all, I need some assistance. I've been out of the C++ game way too long and I need some help getting back up to speed. I'm taking a class where STL is mostly covered...I know alot of...
2
by: Trevor | last post by:
I'd really appreciate it if someone could please help me understand the code below. The only question I have about c_table() is about the t && range( 1 )... line; what does the t && part mean? The...
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
7
by: Mickyd1561 | last post by:
Hey everyone I'm new to this groups thing and thought maybe someone can help me. My problem is that I can't view specific images on only one website. www.baseballu.net is my baseball team's...
19
by: mohammaditraders | last post by:
a program which consists of a class named Student, the class should consists of three data members Name, Ob_marks, Total_marks and two member functions Cal_percentage() which calculate the...
6
by: CD1 | last post by:
#include <string> This code won't compile. There is no variable called "foo2". :)
7
by: sara | last post by:
I have a friend doing some pro-bono work for a non-profit that does job training for distressed kids under DCSS care. He asked me for code to do the following (he's using A2003). I can't find...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.