ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.