LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-03-2022, 11:03 AM   #1
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Rep: Reputation: 51
Exclamation Python syntax problem?


Compare the 2 sources below. They are different only in lines 11-15.

Code 1, formatted in a way I do not want:

Code:
 1 """
 2 Programa mínimo para inserção de dados no SQLite
 3 """
 4 import sqlite3
 5 import sys
 6 from subprocess import call
 7 call( ['rm', '-frv', 'bancoTeste'] )
 8 con = sqlite3.connect("bancoTeste")
 9 bd = con.cursor()
10 
11 bd.execute(
12     "create table tabeleste \
13         (   coleste     text )"
14 )
15 
16 
17 bd.execute("insert into tabeleste values (datetime())")
18 con.commit()
19 con.close()
20 print( 'fim?' )
21 # vim: fileencoding=utf-8: expandtab: shiftwidth=4: tabstop=8: softtabstop=4
Code 2, formatted in the way I want:

Code:
 1 """
 2 Programa mínimo para inserção de dados no SQLite
 3 """
 4 import sqlite3
 5 import sys
 6 from subprocess import call
 7 call( ['rm', '-frv', 'bancoTeste'] )
 8 con = sqlite3.connect("bancoTeste")
 9 bd = con.cursor()
10 
11 bd.execute
12 (
13     "create table tabeleste \
14         (   coleste     text )"
15 )
16 
17 bd.execute("insert into tabeleste values (datetime())")
18 con.commit()
19 con.close()
20 print( 'fim?' )
21 # vim: fileencoding=utf-8: expandtab: shiftwidth=4: tabstop=8: softtabstop=4
Both are valid in Python syntax. But their output is not the same.

For code 1, it is:

Code:
$ p3 min3.py 
removido 'bancoTeste'
fim?
For code 2, it is:

Code:
$ p3 min3.py 
removido 'bancoTeste'
Traceback (most recent call last):
  File "/dev/shm/t/lixo/git/pedalNatural/servidor/min3.py", line 17, in <module>
    bd.execute("insert into tabeleste values (datetime())")
sqlite3.OperationalError: no such table: tabeleste
So, in code 2, the code in lines 11-15 was not executed, and no error is given. But that has an effect in the program's algorithm, which causes the error in line 17, that depends on the table that should have been created.

What do you think about this?
 
Old 07-03-2022, 11:37 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,249

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
It's pretty easy to see the problem when you just go through it line by line in the REPL.

Code:
>>> def p(x):
...     print(x)
... 
>>> p
<function p at 0x7fd05d6392d0>
>>> ("a")
'a'
First, remember that interpreters work line by line. It reads one line, executes that, reads the next line, executes that...

Second, a function is a value that can be returned, passed to other functions, etc. The way it's typically put is that Python has first-class functions. As opposed to C, which has function pointers to carry out the equivalent functionality.

So this line is a no-op that just takes the function's value and throws it out:

Code:
p
And this line is a no-op that creates a value and throws it out:

Code:
("a")
But if you do this:

Code:
p(
Then the opening brace, which is on the same line, tells the interpreter that more lines are coming, and that it should keep track of that until it sees the closing brace. So this works:

Code:
>>> p(
...     "a"
... )
a

Last edited by dugan; 07-03-2022 at 11:05 PM.
 
2 members found this post helpful.
Old 07-03-2022, 11:53 AM   #3
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,628

Rep: Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557

In many languages, statements are terminated with semi-colon. (In some, like JavaScript, the semi-colon is optional and automatically inserted in certain situations).

In Python, statements are terminated by newline characters, unless something explicitly stops that happening (such as unclosed parentheses, etc).

If you want to align parentheses, you can use a backslash to escape the newline - like you would within a string - and thus continue the statement instead of terminating it:

Code:
doSomething \
(
...
)

Last edited by boughtonp; 07-03-2022 at 11:55 AM.
 
1 members found this post helpful.
Old 07-03-2022, 03:09 PM   #4
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Thumbs up

Thank you both very much for the explanations. All the details you said, dugan, are very important, and i am leaving this thread bookmarked, together with other things i am learning about Python.

boughtonp, you practically predicted exactly what I was thinking about testing and asking when i was reading dugan's post. You explained everything I had in mind about it.

 
  


Reply

Tags
python



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Common requirements to get a job as a Python developer, in addition to fluency in Python syntax snowmagician Programming 6 01-29-2020 05:46 PM
I got error while installing python-tk python-psycopg2 python-twisted saili kadam Linux - Newbie 1 09-05-2015 03:03 AM
[SOLVED] Python Installed; bash command: python base.py. gets syntax error in base.py cre84j Linux - Software 4 01-23-2011 04:46 PM
[python] syntax Error : invalid syntax Python_user Programming 2 09-06-2009 12:52 PM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration