J
John Bokma
Leif said:How?
my $sort = $cgi->param( "sort" );
my $query = "SELECT * FROM table WHERE id=? ORDER BY $sort";
Leif said:How?
John said:my $sort = $cgi->param( "sort" );
my $query = "SELECT * FROM table WHERE id=? ORDER BY $sort";
But Python's DB-API (the standard way to connect to an SQL database
from Python) makes escaping SQL strings automatic. You can do this:
cursor.execute('UPDATE foo SET bar=%s WHERE id=%s', ["foo'bar", 123])
Leif said:John said:my $sort = $cgi->param( "sort" );
my $query = "SELECT * FROM table WHERE id=? ORDER BY $sort";
And the equivalent Python code:
cursor.execute('SELECT * FROM table WHERE id=%%s ORDER BY %s' % sort,
[some_id])
You're right, of course, about being *able* to write code with SQL
injection vulnerabilities in Python. But it's not even close to being as
easy as in PHP.
Is that wrong, and how should I have been supposed to know that this is
bad syntax? No doc I have seen actually has told me so.
John Bokma said:Why?
Python programmers
seldom have to worry about that - there's usually only one [obvious]
way to do things, so you just do it that way.
I doubt that :-D. I have never seen a programming language that forced
me in such a way :-D.
I doubt that too ;-)
Yeah, Perl programmers are extremely slow ;-)
In most cases, why worry? If speed is an issue you should probably have
picked a different programming language in the first place (for that
part of the code).
Use the one that is the easiest to read and understand by yourself.
That's how I program Perl, and that's how I am going to program Python.
An overloaded toolbox doesn't mean that if you have to hang a painting
on the wall that you have to think for 6 hours which tool you should
use. You just take the one that does the job in a way you feel
comfortable with. I know that the Perl mantra is often misunderstood,
but in my experience only by people who have very little experience with
the language.
Peter said:I'm bewildered why you haven't mentioned magic quotes. A one line
change to the configuration file can render your PHP site almost
entirely immune to SQL injection attacks.
Leif said:John said:my $sort = $cgi->param( "sort" );
my $query = "SELECT * FROM table WHERE id=? ORDER BY $sort";
And the equivalent Python code:
cursor.execute('SELECT * FROM table WHERE id=%%s ORDER BY %s' % sort,
[some_id])
You're right, of course, about being *able* to write code with SQL
injection vulnerabilities in Python.
But it's not even close to being as
easy as in PHP.
Peter said:Leif said:John said:my $sort = $cgi->param( "sort" );
my $query = "SELECT * FROM table WHERE id=? ORDER BY $sort";
And the equivalent Python code:
cursor.execute('SELECT * FROM table WHERE id=%%s ORDER BY %s' % sort,
[some_id])
You're right, of course, about being *able* to write code with SQL
injection vulnerabilities in Python. But it's not even close to being
as easy as in PHP.
I'm bewildered why you haven't mentioned magic quotes. A one line
change to the configuration file can render your PHP site almost
entirely immune to SQL injection attacks.
Leif said:PHP's magic quotes is one of the most poorly-designed features I can
think of. Instead of magically escaping only strings which will actually
be passed to a database (like Python's DB-API does), it escapes every
string that comes from the user, meaning that strings which will be sent
back to the user have to be manually unescaped.
Even worse, since it can be turned on and off, code which is designed
for a magic_quotes=on environment will become seriously vulnerable when
moved to an environment with magic_quotes on. Security-related features
should never be toggleable!
Mike said:One of the stated conditions for more than one Perl position I've
looked at.
Try programming in older Python for a while. You'll quit doubting it.
Nah, they aren't slow. They just have to worry about more things than
the Python developers.
You apparently haven't been programming in Python very long.
If speed
is an issue, you should probably pick a different
algorithm. Well-written Python programs use the C-coded parts of
Python for the time-critical part of the code.
Python.
The thing is, in Python there usually is one obvious way to do
things. That's changed in recent years as improvements have been added
without dropping the features they replaced so as not to break old
programs.
The problem with using "the way you feel comfortable with" is that it
may not be comfortable for the maintainer - even if that's you six
months from now.
My Perl evolved from very shell script like (lots of
backticks and passing around the full text of files)
to one more
C-like as I used the language. That change was largely driven by
performance concerns as my understanding of the language grew.
John Bokma said:That's weird, since some can differ between versions of Perl like for
example a map in a void context. And I am sure Python has now and then
improvements which make one operation suddenly 10% faster in a new
version.
So it tells you a lot about the people providing the position ;-)
Again I doubt that. I doubt that if I give 100 old Python programmers a
certain computer science problem I will see 100 exactly the same
solutions, which when benchmarked run equally fast.
Do you have references to this? I would love to see if indeed 100 Python
programmers do implement, say 5 CS tasks faster compared to 100 Perl
programmers, on average.
Why are those time critical parts not written in Python ;-)
I know to little about Python, especially about old Python, to have an
idea of what you mean. I only saw, quite recently, someones solution to
a problem that used 30 lines of Python, and I am sure I could have
written it in 3-4. And I doubt that's because he was using, or I was
thinking in, new Python
:-D.
After 5 years one gets comfortable with most constructions in a
language. I am more often uncomfortable with the way someone translates
a problem into code. And there is no way how you can force people with a
language that is, to do that the same, not even close.
I am sure you can do that with Python too.
Yup, in Perl one should program Perl, not bash, not C. Same for Python.
Mike said:The things is - it doesn't matter nearly as much in Python. Since
there's only one obvious way to do most simple things, that's the one
that most programmers will choose, and that's the one the developers
will worry about making faster.
Yeah - they expect their people to be on top of such things. I've
never run across a Python position where knowing which of several ways
was faster was an important criterion - because there usually aren't
several ways.
Depends on the problem. If it's one of the things for which Python has
an obvious solution (sort a list; split a string on whitespace; pull
select list elements based on a criteria of some kind; search a file
for lines with a given word in them; etc.) you'd get back the same
answer from almost all of them.
As I've said a number of times, this has changed lately as new
features have been added without removing their older counterparts.
I'm talking about the people developing the language, not the ones
developing in the language. And this is just my impression from
watching the developers lists.
Because then they'd be slower. A good scripting environment has lots
of operations available that are coded at a low level. Since this code
is usually hand-tuned and optimized over time (like the dictionary
lookup or string operations in Python, or regular expressions in Perl
- I'm not sure about the RE library in Python), it may well be faster
than what you'd write in the low level language if you did it
yourself.
Just because there's only one obvious way to do things doesn't mean
it's obvious to everyone.
Witness the recent spate of people asking
for "goto".
Of course not. The best you can do is provide one obvious way to do
things, and then hope most people find that.
That hasn't happened in 9 years of Python.
Some of the idioms have
changed because the language has changed, but not because my
perceptions/expectations/etc have changed.
Right. But Perl makes it *easy* to program in bash or C while staying
in Perl, and provides no obvious drawbacks from doing so - all because
it's trying to be comfortable to sh and C programmers by providing
ways to do things that those users are comfortable with. Trying to
write sh or C in Python is painfull, and you usually run into
problems/drawbacks fairly quickly - because that's not the obvious way
to do things.
John Bokma said:And what makes you doubt it would be different with Perl? :-D
( sort @alist, split ' ', $astring, grep criteria, @list, etc )
The only variation I expect for the above Perl solutions is the use of
(), and the first argument of split. However, the last one might show
some exotic solutions![]()
I have no idea, since I read neither list. I have the idea that Perl is
indeed more fat, especially since the (too big) number of built in
stuff, that Python has in libraries (where it should be IMNSHO).
One could say (and should) that the regular expression thingy in Perl is
a language on its own![]()
But you probably agree, that if speed is an issue, and the programmer's
skills are not the issue, that the only thing one can do is code it in a
different language (probably by another programmer).
My point. Moreover, most skilled Perl programmers (or at least the ones
I know) fall back to a shared common set of idioms, which probably are
not always the fastest one (which I consider a good thing)). So to them
there are often obvious ways to do things. Put them together, and for
the small problems you gave they very likely will come up with similar
results.
So restrictions on the number of ways fails for beginners, because: life
will find a way. And skilled programmers don't need it, because they
somehow stick together.
I was a witness, moreover I stated my opinion, which in short stated
that there is nothing wrong with goto as a statement. Keeping a
statement from a language because beginners don't understand it, and
write bad code with it, is wrong. Moreover, every time I see some kind
of protection in a language, I see people who consider them safe, and
make other, equally bad (or worse) mistakes. (See my other replies in
this thread regarding the special "protection" PHP has).
You either understand why something is bad at a given moment, or not. If
a language must make up for the understanding a programmer lacks,
something is very very wrong.
I doubt that Python needs a goto, since I am sure it has enough "syntax
sugared" versions of goto to solve most, if not all things that one
needs a goto for.
Exactly.
Beginners get lost anyway, experienced programmers don't need it
That was not what I wrote: I am sure one can program that way in Python
too.
In 9 years? I learned so much in 9 years about programming that I
probably am ashamed to show Perl code I wrote 9 years back(Even if
I left out the Perl 4 syntax). Moreover, I think that my current Perl
code is quite different from 3-4 years back. And no, not all is related
to the "there are more ways to do things". I changed my mind on certain
things (mostly style issues), learned new modules (some are really new,
others I just didn't know about), but also: I learned new programming
techniques, or other ways to look at a problem, and hence, write a
different solution (but using the same idioms as 3-4 years back).
But again, it doesn't keep people who want to do this.
Also, Perl borrows a lot of its syntax from C, which Python (as far as I
have seen) does extremely little, if at all. So it's already easier to
program C-like in Perl, by design, even if Perl was as "restrictive" as
Python.
If the current direction of Python means relaxing it a bit, I personally
think that is a good thing. Sometimes more than one way makes it
possible to introduce a construction that's well known in another
language, and turn it into an idiom for the "more than one way"
language, and hence, teach "new" ideas to people.
And my quick peek at http://www.xml.com/pub/a/2004/03/31/pycon.html
gives me the impression that the current direction of Python is to make
such things available (for example more lazy, which I like thanks to my
functional programming experience)
Mage said:I don't think so. Bad programmers are able to write bad programs in any
language. Maybe there are more bad php programmers than python
programmers and the 70% of the dynamic world wide web is copied from
user comments in the php.net/manual. However one of the worst cases is
the sql injection attack. And sql injections must be handled neither by
php nor by python but by the programmer.
Is anyone capable of providing Python advantages over PHP if there are
any?
Roman said:# (e-mail address removed) / 2005-04-23 15:53:17 +0200:
*I* wouldn't consider doing anything like that said:The comparison found there is biased, the author is a Python
partisan.
Mike said:Perl's "There's more than one way to do it" attitude.
In the one case where perl provides you two ways to do things, you
chose the one that doesn't solve the problem. You want map, not grep.
You also chose the wrong way to do things globally, by leaving off the
optional parens on the function invocations. That makes the list a
PITA to parse,
in that you have to know the number of arguments to
each function in order to parse the list you provided.
Consider the python version:
(alist.sort(), astring.split(), [x for x in alist if
criteria(x)])
This list can be parsed by a machine. Given proper bindings for all
the variables, Python will turn it into a tuple.
N.B.: split(' ', $astring) is wrong as well. In this case, Perl
doesn't provide the right tool for the job; you have to build it from
parts.
And those trivial variations are enough that you chose the wrong
ones. And you're (by your own admission) an experienced Perl
programmer.
It is. For what Perl was designed for, having that be part of the
grammar of the language makes sense. For a general-purpose programming
language, I'm not so sure.
I don't agree. The better alternative is to look for a faster
algorithm.
Now, I could have recoded it in C and gotten *much* faster. But then
I'd have to deal with all the dynamically allocated objects (millions
of them!) by hand,
My experience with Perl is just the opposite. Everytime I have to
maintain Perl code, I come across a different set of idioms - with no
obvious downside to them. You can, of course, define "skilled
programmers" as those who use your idiom set. This makes the set of
skilled Perl programmers very small. That few programmers are skilled
is a downside of TMTOWTDI, as the only time I see new idioms in Python
are newbies doing very odd things here.
part of the grammar of the language, and are a recent addition. It's
not clear how Python should evolve with them added to the language.
I, on the other hand, feel that adding something to the language just
because you can is a *really* bad idea.
you run into it in someone elses code. I never saw a good use case for
goto in Python (as opposed to C, where it's clearly needed).
There's a difference between "making up for the understanding a
programmer lacks" and "handicapping the programmer". Missing gotos is
like missing explicit pointer manipulation.
Carefully designed
features added to the language (or restrictions removed from it) can
provide the same functionality as those features, without exposing
programmers to all the potential bugs that can occur when those
features are used.
Adding something that doesn't extend the language and creates
potential bugs is a *mistake*. You run that risk every time you add
just another way to do something.
I disagree. If there's not one obvious way to do things, experienced
programmers tend to choose the one they are most comfortable with, and
will have to work out what all the others mean. This has been my
experience with Perl and Common LISP, and from what I hear is true of
C++ as well.
On the other hand, my experience with languages that provide one
obvious way to do things (Python, Scheme, Eiffel) is that experienced
programmers tend to find that way, and beginners quickly become
frustrated when they fight the language.
No, you said "one can do that with Python", leaving the word "that"
open to interpretation. I assumed you meant "migrate from writing sh
to writing C", which, as I said, hasn't happened.
If you meant writing sh or C in Python, try it.
You'll find it clumsy and frustrating.
Python wasn't *designed* to make it comfortable to
write that way. Perl was - just a couple of more ways to do things.
The only one of those that really matters is "changing your mind on
certain things".
The only way to do that is if there's more than one
way to do those things in the first place.
My Perl style changed
radically over the years. My python style has as well, but only
because the language has changed as time passed. In the cases where a
new construct/module hasn't been added, the idiom that was obvious
with Python 1.4 is still the right choice with Python 2.4. With Perl,
there's no obvious right choice,
Um - this sentence isn't complete. It doesn't keep those people from
*what*?
A langauge can make it easy to write ugly code, or it can make it hard
to write ugly code.
After all, "You can write FORTRAN in any
language(*)". If I have to maintain the code, I'd *much* prefer that
the language make it hard to write ugly code, as I'm that much less
likely to get ugly code to maintain.
Nonsense.
TMTOWTDI makes it easy to write
ugly code. TOOWTDI makes it hard to write ugly code.
If Perl only borrowed a lot of it's syntax from C,
this wouldn't be a
problem. But it borrows a lot of it's syntax from sh as well,
sometimes supporting both (because TMTOWTDI). If it had done one or
the other, it would have been a better language.
I will agree that adding new idioms from other programming paradigms
is a good thing (see <URL:
http://en.wikipedia.org/wiki/Programming_paradigm >). In theory, the
reason for that is that changing paradigms changes the set of things
you do, so you aren't adding more ways to do things, you're adding
more things to do.
The reality is that such changes often do introduce new ways to do
things that already exist.
There have already been threads where it's
been argued (by others - I just read them) that this is detrimental to
Python.
Right. But read <URL: http://www.python.org/peps/pep-3000.html > and
you'll see that many of the things that are now redundant are
scheduled to go away when we break backwards compatability. The
philosophy is still that "There should be one-- and preferably only
one --obvious way to do it." (from "import this"), even if we are
drifting from that in the current implementations.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.