MudBytes
» MUDBytes Community » Coding Discussions » Coding and Design » python for scripting
Pages: << prev 1, 2 next >>
python for scripting, why not?
quixadhal
Wizard






Group: Members
Posts: 1,256
Joined: Oct 17, 2007

Go to the bottom of the page Go to the top of the page
#16 Posted Jul 16, 2009, 4:29 am

David Haley said:

Code (text):
1
2
3
4
5
6
7
8
9
 
def f(x, y):
if x > 2:
x = x + 1
x = x * y
return x
 

The example is contrived, but the point is made perfectly. Where do you put the line x = x * y? Do you put it inside the if block or not? In other words, do you assign x*y to x only when x > 2, or always?


I don't consider that a downside.  Why do we bother to indent anyways?  Because it makes the code clear and easy for others to follow.  Except nobody seems to be able to agree on the stupidly nitpicking details of it.

Code (text):
1
2
3
if(x){foo();}else{bar();}

Code (text):
1
2
3
4
5
6
7
if(x) {
  foo();
} else {
  bar();
}

Code (text):
1
2
3
4
5
6
7
8
9
10
if(x)
{
  foo();
}
else
{
  bar();
}


All perfectly valid C code, but totally different in appearance.  In such tiny examples, it doesn't matter, but go look at a decent sized codebase where you have a dozen levels of indentation, half of which use one style, half of which use another, and tell me again, how having a language-enforced level of consistency is such a horrible idea.

In the contrived example David presented, we can't know which parts he meant to include in the if statement, but the author presumably knew.  The same can be said for this C code....

Code (text):
1
2
3
4
5
6
7
if(x)
foo();
bar();
if(y)
foobar();


Sure, it SAYS to always call bar(), but is that what the author meant to do, or did he just forget to put braces around the foo();bar(); calls?  Does having the begin/end symbols help you there?  In they python example, the compiler will probably fail to compile that, but if you indented everything below the def line one level, it would be valid... and just as confusing as the C example here.

Scanning a chunk of code that's hundreds or thousands of lines long, it's much easier to spot a change in the indentation level than it is a missing brace or END statement.  If the language requires you to indent, you'll very quickly learn to indent.  If not... well, I've dealt with a lot of really ugly (but functional) perl code that makes me wish it had been python.

Of course, this isn't an argument that can be won... but I feel compelled to point out that lack of enforced indentation can lead to exactly the same problems that forced indentation can.  You can mitigate this somewhat if your language always requires begin/end symbols, but then people would complain about how cumbersome it is to write quick one-liners.  :surprised:
.........................
http://i302.photobucket.com/albums/nn96/quixadhal/Alelord_banner.png

Scandum
Wizard






Group: Members
Posts: 1,105
Joined: Aug 8, 2006

Go to the bottom of the page Go to the top of the page
#17 Posted Jul 16, 2009, 8:52 am

quixadhal said:
Of course, this isn't an argument that can be won... but I feel compelled to point out that lack of enforced indentation can lead to exactly the same problems that forced indentation can.  You can mitigate this somewhat if your language always requires begin/end symbols, but then people would complain about how cumbersome it is to write quick one-liners.  :surprised:

I've picked up the habit to always use braces which probably saved me a lot of trouble over the years.

Auto indentation in Python is possible because Python will know the internal indentation level, so it could write out the tokenized code indenting with 2 spaces, 1 tab, 4 spaces, etc. It'd be more like standardizing the indentation than adding indentation. On the other hand, I don't think the Python developers would understand the need for this functionality since it only makes sense for online development.

David Haley
Wizard






Group: Members
Posts: 5,730
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#18 Posted Jul 16, 2009, 9:34 am

Yes -- you can solve this problem in C/C++/Java etc. by simply always using braces. It's a good habit to pick up.

Lua doesn't give you a choice, you must use explicit block terminators, e.g.:

Code (text):
1
2
3
4
5
6
7
 
if x > y then
  return 2
end
 


Quote:
Auto indentation in Python is possible because Python will know the internal indentation level, so it could write out the tokenized code indenting with 2 spaces, 1 tab, 4 spaces, etc. It'd be more like standardizing the indentation than adding indentation. On the other hand, I don't think the Python developers would understand the need for this functionality since it only makes sense for online development.

No, as I said, it is impossible in general to automatically indent Python code that isn't already indented. The example I gave is enough to show that. However it is true that the editors can make pretty good guesses most of the time, again as I said. But this is quite different from automatically indenting Python code in general.

quixadhal said:
In they python example, the compiler will probably fail to compile that, but if you indented everything below the def line one level, it would be valid...

I don't think it would be, as you must start a new indentation level after the if statement, or put the if-block on the same line.

quixadhal said:
In such tiny examples, it doesn't matter, but go look at a decent sized codebase where you have a dozen levels of indentation, half of which use one style, half of which use another, and tell me again, how having a language-enforced level of consistency is such a horrible idea.

Python doesn't enforce the style of indentation, just that indentation separates blocks. Nothing prevents one developer from using four spaces, another from using 3 spaces, another from using tabs, another from putting things on the same line as the if statement, and so on and so forth. The rule is that one block must have the same indentation through the block, but different blocks in the same function can mix indentation levels...
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Scandum
Wizard






Group: Members
Posts: 1,105
Joined: Aug 8, 2006

Go to the bottom of the page Go to the top of the page
#19 Posted Jul 16, 2009, 11:31 am

David Haley said:
Quote:
Auto indentation in Python is possible because Python will know the internal indentation level, so it could write out the tokenized code indenting with 2 spaces, 1 tab, 4 spaces, etc. It'd be more like standardizing the indentation than adding indentation. On the other hand, I don't think the Python developers would understand the need for this functionality since it only makes sense for online development.

No, as I said, it is impossible in general to automatically indent Python code that isn't already indented. The example I gave is enough to show that. However it is true that the editors can make pretty good guesses most of the time, again as I said. But this is quite different from automatically indenting Python code in general.

You're mistaken, for example:
Code (text):
1
2
3
4
5
 
if 1 + 1 == 2: print "foo"; print "bar"; x = 42
 

Could be auto indented as:
Code (text):
1
2
3
4
5
6
7
8
 
if 1 + 1 == 2:
    print "foo"
    print "bar"
    x = 42
 


There's really no 'auto indentation' in the way you speak off (except for editors), because code is always indented by the parser according to the rules of the language. I understand your confusion, perhaps try to think of it as 'standardized indentation', though it doesn't matter a whole lot how you think of it. This because the concept is implicit when tokenized data is written back to file as untokenized data.

Last edited Jul 16, 2009, 11:32 am by Scandum
David Haley
Wizard






Group: Members
Posts: 5,730
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#20 Posted Jul 16, 2009, 11:34 am

My claim is that in general it is impossible. It is not logically correct to dispute that claim by showing one example where it is possible. There are many examples where it is possible. But it is not always possible, as my very short example above showed. There is no confusion on my part.

By the way, the parser is most certainly not the thing that is indenting the code. The parser is the thing using indentation to understand where blocks start and stop.
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Kelvin
Magician






Group: Members
Posts: 61
Joined: May 18, 2006

Go to the bottom of the page Go to the top of the page
#21 Posted Jul 16, 2009, 11:37 am

I saw this out on the intarweb at one point and thought it was amusing (and true):
Quote:

Useless programmers complain about indentation in Python. Those same programmers complain about semicolons in C.

I will say that the Forestry department here teaches Python to Forestry students in the GIS field. Mind you, these students have never wrote a line of code beforehand, and end up doing some pretty neat stuff. If some of the people in our Forestry department are learning and putting Python to great use, I'm positive that your semi-intelligent (at least) builders can figure it out :)

It's all a matter of preference. I really like the enforced indentation, but it's a matter of preference and no one philosophy will ever be "right". Let's not get into a holy war here.
.........................
Evennia: Python + Twisted + Django + MUD = Win
Embrace the snake

Scandum
Wizard






Group: Members
Posts: 1,105
Joined: Aug 8, 2006

Go to the bottom of the page Go to the top of the page
#22 Posted Jul 16, 2009, 11:39 am

David Haley said:
By the way, the parser is most certainly not the thing that is indenting the code. The parser is the thing using indentation to understand where blocks start and stop.

The sub discussion is about a parser reading a script from file.. read closely.. and then the parser writing the script back to file.. read carefully.. most scripting languages do not provide this functionality.. thank you for your time.

Last edited Jul 16, 2009, 11:40 am by Scandum
David Haley
Wizard






Group: Members
Posts: 5,730
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#23 Posted Jul 16, 2009, 11:48 am

Many scripting languages provide that functionality; I'm not sure why you think they don't. The point is that in general automatic indentation of Python source code is impossible. If you already have the source code in a parse tree, then obviously the question of what goes in which block is moot, because ... you have the parse tree. If you were exclusively talking about doing this from a parse tree, it would have been a little bit helpful to make that clear, because obviously the other people in the discussion did not understand what you meant given how you phrased it. "thank you for your time", indeed!
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Kelvin
Magician






Group: Members
Posts: 61
Joined: May 18, 2006

Go to the bottom of the page Go to the top of the page
#24 Posted Jul 16, 2009, 2:52 pm

Of course if you want to use braces in Python, just do this:
Code (text):
1
2
3
4
5
 
from __future__ import braces
 
.........................
Evennia: Python + Twisted + Django + MUD = Win
Embrace the snake

David Haley
Wizard






Group: Members
Posts: 5,730
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#25 Posted Jul 16, 2009, 3:21 pm

Nice easter egg. :smile:
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Pages:<< prev 1, 2 next >>

Valid XHTML 1.1! Valid CSS!