Domain: python.org
Stories and comments across the archive that link to python.org.
Comments · 1,513
-
Re:Honestly?
Python 3.0 (a.k.a. "Python 3000" or "Py3k") is a new version of the language that is incompatible with the 2.x line of releases....
http://www.python.org/download/releases/3.0/
Is Phyton a fork of php? it seems like it has some similarities anyway...
-
Ensure tidy code for reviews
As a result, I've lost hundreds of hours in code reviews because some pedant was more interested in picking nits over whitespace than actually reviewing my algorithms.
The places where I worked used a simple guide line: the author has to clean up trivial issues before submitting an item for review. In case of code, this means ensuring style conventions.
Which in practice is trivial for reasonably popular programming languages:
First, you rarely have to invent your own style conventions. Instead, use existing standards. For instance, there are standard coding conventions for Java and Python.
If some guide lines do not make sense for your shop, simply change them to fit your needs. For example, PEP8 recommends a maximum line width of 79 characters. However, all our developers work a horizontal resolution of 1920, so we allow 132 characters. We also changed naming conventions from the loud and baroque SOME_CONSTANT to the calm SomeConstant.
Next, there are tools to check these conventions such as CheckStyle and flake8. Often you can even find tools to reformat source code consistently or automatically fix trivial issues like missing blanks, for example autopep8.
Some shops inject these tools in the commit hook of their version management and make it reject any code that does not conform. Baring that, the review moderator is required to run the style checks on the review item. If it turns out that the author still left style issues in the code, the moderator returns the item to the author and requests to clean it up.
That way, the reviewers always get tidy code and can focus on comprehensibility, maintainability and algorithms.
.
-
Ensure tidy code for reviews
As a result, I've lost hundreds of hours in code reviews because some pedant was more interested in picking nits over whitespace than actually reviewing my algorithms.
The places where I worked used a simple guide line: the author has to clean up trivial issues before submitting an item for review. In case of code, this means ensuring style conventions.
Which in practice is trivial for reasonably popular programming languages:
First, you rarely have to invent your own style conventions. Instead, use existing standards. For instance, there are standard coding conventions for Java and Python.
If some guide lines do not make sense for your shop, simply change them to fit your needs. For example, PEP8 recommends a maximum line width of 79 characters. However, all our developers work a horizontal resolution of 1920, so we allow 132 characters. We also changed naming conventions from the loud and baroque SOME_CONSTANT to the calm SomeConstant.
Next, there are tools to check these conventions such as CheckStyle and flake8. Often you can even find tools to reformat source code consistently or automatically fix trivial issues like missing blanks, for example autopep8.
Some shops inject these tools in the commit hook of their version management and make it reject any code that does not conform. Baring that, the review moderator is required to run the style checks on the review item. If it turns out that the author still left style issues in the code, the moderator returns the item to the author and requests to clean it up.
That way, the reviewers always get tidy code and can focus on comprehensibility, maintainability and algorithms.
.
-
Ensure tidy code for reviews
As a result, I've lost hundreds of hours in code reviews because some pedant was more interested in picking nits over whitespace than actually reviewing my algorithms.
The places where I worked used a simple guide line: the author has to clean up trivial issues before submitting an item for review. In case of code, this means ensuring style conventions.
Which in practice is trivial for reasonably popular programming languages:
First, you rarely have to invent your own style conventions. Instead, use existing standards. For instance, there are standard coding conventions for Java and Python.
If some guide lines do not make sense for your shop, simply change them to fit your needs. For example, PEP8 recommends a maximum line width of 79 characters. However, all our developers work a horizontal resolution of 1920, so we allow 132 characters. We also changed naming conventions from the loud and baroque SOME_CONSTANT to the calm SomeConstant.
Next, there are tools to check these conventions such as CheckStyle and flake8. Often you can even find tools to reformat source code consistently or automatically fix trivial issues like missing blanks, for example autopep8.
Some shops inject these tools in the commit hook of their version management and make it reject any code that does not conform. Baring that, the review moderator is required to run the style checks on the review item. If it turns out that the author still left style issues in the code, the moderator returns the item to the author and requests to clean it up.
That way, the reviewers always get tidy code and can focus on comprehensibility, maintainability and algorithms.
.
-
Re:Python
I second Python: just type in the pseudocode you'd write on a piece of paper, and there is a good chance that it will work just like that in Python.
2D gaming using SDL (and OpenGL 3D, but you have to do the hard work yourself): Pygame
3D drawing/animation/gaming: Blender 3D
(I started by gaming, because that's a fun way to learn a language quickly)Web: Django
Co-routines: Stackless Python
Maths: NumPy and SciPy
Networking: TwistedThat just scratches the outside of it, but have a look at the above to get an idea of the language.
And Python's documentation is quite good: brief, but everything you need is there - you just need less than you would expect at first. Here are some good tutorials:
Official Python Tutorial
Dive into Python
How to think like a computer scientist?Hmmm, looks like I've turned into a Python fanboi... Be careful if you try Python, you could fall for it.
-
I found their nest....
Also very long python there
.... -
Re:Python VS PHP
I do, however, disagree with Guido's statement that "Python is fast enough". Whether it is or not depends entirely on what you're doing. For my purposes I don't think a 12-core computer optimally programmed in assembler would be "fast enough".
It's faster to write code in Python than in C or C++ or Pascal or Java. But Python isn't "fast enough" unless your program is I/O bound. And it's inability to handle multiple processing uints gracefully is a real problem. (Not that anyone has a decent answer to that except the dataflow people and the pure functional language people.) Multiple core machines are now the rule rather than the exception, so the GIL is no longer acceptable. Even Ruby attempts to address that, though they didn't really follow through after considering their library situation.
You're absolutely correct that whether a language implementation is "fast enough" depends totally on the problem being solved. If the program is I/O bound, as most are, Python and many other languages are fast enough. Even if part of the problem is CPU bound, there is usually a lot of code that is not, which is why hybrid C/Python solutions are so common. For example, NumPy can take advantage of multiple CPUs when doing matrix calculations, allowing one do heavy, fast number crunching without writing a line of C.
If you need to do CPU-bound processing in Python code, threads won't help if you're using CPython, but they might if you're using Jython which is JVM implementation that has no GIL. Alternatively, you can use the multiprocessing module which avoids the GIL and a lot of other potential problems with threads at the cost of having to explicitly communicate between processes rather than share data structures directly.
-
Re:Python VS PHP
Python has its own set of problems.... The most recent that I had to contend with is the lack of any decent SOAP library and a lack of a decent postgresql library. It seems to have an abundance of half baked libraries/extensions, and the python 3 fiasco has just made it even worse.
Python is a great language in itself, but getting any real work done with it is an exercise in reinventing the wheel.
There are certainly plenty of things that could be better about Python, but you're going to need to be a lot more specific with your rants. What is not decent about psycopg2? What libraries are "half-baked" and why? What exactly is a fiasco about Python 3? If you do a lot of reinventing of the wheel, you may not have encountered the extensive standard library or abundance of good libraries at PyPi.
-
from collections import Counter as bag
When you say a "bag", how big a bag are you talking?
Might it be this bag implemented in Python as collections.Counter?
-
Re:Python Indentation: Style is the language
Ugh, I keep forgetting that
/. doesn't display newlines and there's no edit button. Here it is again in a more readable format.Learn to use the tool before blaming it: http://www.python.org/dev/peps/pep-0008/ [python.org]
The best way to describe Python is that it is a real tool. Quoting http://cristal.inria.fr/~weis/info/commandline.html [inria.fr], it is not a "homeowner's toy" like Java which holds your hand with type-checking, etc. but a drill that does the job you tell it to. If you want to shoot yourself in the foot, Python's not going to lock itself and say to you in a calm, reassuring voice, "Hey, are you all right? I'm sure you didn't mean to do that." It's going to f*cking blow away your feet, because that's what it was designed to do and that's what you used it to do. Python doesn't tell you what you should do, it does what you tell it to do. If you decide to mix tabs and 2-spaces and 4-spaces and 2-space-tab-3space, Python lets you do that. Don't come crying to mommy if it blows up on you though.
Another thing is to use a proper IDE/text editor. Any competent IDE can easily replace tabs with spaces or vice versa, as well as highlight syntax errors. The fact that you have so much trouble with it suggest that you are not using such an IDE. I would suggest Vim with some of the very good available Python plugins and syntax highlighting, as well as Syntastic for syntax errors.
I have had no problems copy pasting code from sites I've found with google. Perhaps the problem is that you don't know how your web browser/OS's copy pasting works? Or that the code wasn't properly formatted properly in the first place, in which case I wouldn't copy paste without reformatting it anyway, no matter the language.
-
Re:Python Indentation: Style is the language
Learn to use the tool before blaming it: http://www.python.org/dev/peps/pep-0008/ The best way to describe Python is that it is a real tool. Quoting http://cristal.inria.fr/~weis/info/commandline.html, it is not a "homeowner's toy" like Java which holds your hand with type-checking, etc. but a drill that does the job you tell it to. If you want to shoot yourself in the foot, Python's not going to lock itself and say to you in a calm, reassuring voice, "Hey, are you all right? I'm sure you didn't mean to do that." It's going to f*cking blow away your feet, because that's what it was designed to do and that's what you used it to do. Python doesn't tell you what you should do, it does what you tell it to do. If you decide to mix tabs and 2-spaces and 4-spaces and 2-space-tab-3space, Python lets you do that. Don't come crying to mommy if it blows up on you tough. Another thing is to use a proper IDE/text editor. Any competent IDE can easily replace tabs with spaces or vice versa, as well as highlight syntax errors. The fact that you have so much trouble with it suggest that you are not using such an IDE. I would suggest Vim with some of very good Python plugins and syntax highlighting, as well as Syntastic for syntax errors. I have had no problems copy pasting code from sites I've found with google. Perhaps the problem is that you don't know how your web browser/OS's copy pasting works? Or that the code wasn't properly formatted properly in the first place, in which case I wouldn't copy paste without reformatting it anyway, no matter the language.
-
Python Indentation: Style is the language
Python Reference Manual: 2.1.8 Indentation: Leading whitespace (spaces and tabs) at the beginning of a logical line is used to compute the indentation level of the line, which in turn is used to determine the grouping of statements. First, tabs are replaced (from left to right) by one to eight spaces such that the total number of characters up to and including the replacement is a multiple of eight (this is intended to be the same rule as used by Unix). The total number of spaces preceding the first non-blank character then determines the line's indentation. Indentation cannot be split over multiple physical lines using backslashes; the whitespace up to the first backslash determines the indentation.
-
Re:Remember the old addage
Python 3.3 didn't add a "yield" keyword, it added a "yield from" construct. Python has had "yield" since version 2.2. Python also has type annotations that "don't do anything": http://www.python.org/dev/peps/pep-3107/
-
Re:Weak third-party library support
I agree with the spirit, but not the facts:
wxPython, Django, Twisted... these are not 'niche' modules.
-
Re:Python 2.7.3 is the new IE6
While I like some of the changes in python3, breaking compatibility with python2 was a huge fail. [...] Python 2.7.3 is the new IE6.
How very hipster of you.
But, you're simply wrong. If you had bothered to do even the slightest bit of reading, you'd know full well that the Python creators never intended for Python 3 to replace 2 overnight. The break in compatibility was done deliberately, and with a great deal of thought. There are a number of things fundamentally wrong with Python 2 that can't be fixed without breaking backward compatibility. I, for one, am grateful for Guido and crew for doing so as it will mean a much cleaner and more sensible language.
Python 3 readily available in any OS that matters and can be installed right next to Python 2. Your favorite library isn't supported in Python 3? If it's proprietary, bug the vendor. If it's open source, patches are welcome!
We are in a transition period. The only ones that are bothered by all of this are those who want the entire software and language development world to fit into their neat and tidy little idea of how everything should work. If it really bugs you that much, switch to Perl.
-
LauncherAnonymous Coward wrote:
The launcher is added with 3.3
Thank you. Side-by-side on Windows was broken in 3.0, 3.1, and 3.2, but now that it appears fixed, one more obstacle to adoption of Python 3.3 is out of the way.
-
Re:Undisclosed new feature
In case anyone is wondering, many programs should perform better under Python 3.3 than under 3.2, due to the new way of storing Unicode strings:
The memory usage of Python 3.3 is two to three times smaller than Python 3.2, and a little bit better than Python 2.7, on a Django benchmark.
Benchmarks that focus on certain types of string-operations have seen slowdowns, but real-world applications (such as Django web applications) should benefit from this change. (And real-world applications that perform intensive and performance critical string manipulations should use PyPy.)
-
Re:Python 2.7.3 is the new IE6
What mistakes are you referring to? What was so broken in Python 2 that required a completely incompatible Python 3 as a fix?
- Some changes to clean up the syntax parsing by removing odd or obsolete areas such as the overly-clever 'print' syntax which required special parsing rules
- Old-style classes duplicated the new-style classes
- Changing many parts of the language to return iterators or generators rather than lists
- Well defined Unicode handling as standard throughout the language allows a proper differentiation between bytes and strings
The first three don't have too much of an impact to anyone who does not write particularly odd code, or use very old idioms - however they are strictly incompatibilities in that valid Python 2 code won't run. The last does have impact, but its benefit in terms of proper Unicode handling is too great to ignore.
Some people might prefer a language to acquire a growing tail of obsolete features that partly duplicate newer features. This means the syntax becomes increasing convoluted in an effort to fit both old and new in, and there is a growing list of corner cases where old APIs and language features collide with the new ones. For example, fixing the Unicode issues by duplicating every part of the standard library that deals with strings with a Unicode-aware version would work, but would be fairly bloated and confusing for beginners. GvR evidently felt that it was time for a clean-out.
-
Re:Python 3 and its useYou say that, PEP 394 -- The "python" Command on Unix-Like Systems clearly defines this:
This PEP provides a convention to ensure that Python scripts can continue to be portable across *nix systems, regardless of the default version of the Python interpreter (i.e. the version invoked by the python command).
- python2 will refer to some version of Python 2.x
- python3 will refer to some version of Python 3.x
- python should refer to the same target as python2 but may refer to python3 on some bleeding edge distributions
Pretty clear and standardised to me.
-
Re:Why are people still using this?
What the fuck do you think you were using when you did the rest of the code in Python? Cython is Python.
Well, you just set off the "I don't know what I'm talking about" alarm. Cython is a derivative of Pyrex, neither of which should be confused with CPython. And of course CPython is not Python either, it's the reference implementation of an interpreter for the Python language.
-
Re:I'd be happy if I could just use variable names
Just went ahead and tried Python again. Doesn't work.
Are you using Python 2.x? That's ASCII only. Python 3k is Unicode.
Think I also tried Javascript recently and it was broken, too.
That would be a non-conforming implementation of the language - per spec, it should permit "any character in the Unicode categories: “Uppercase letter (Lu)”, “Lowercase letter (Ll)”, “Titlecase letter (Lt)”, “Modifier letter (Lm)”, “Other letter (Lo)”, or “Letter number (Nl)”.".
And I know C++ complains, at least with g++.
C++ is kinda funny. It permits Unicode identifiers as of C++11, but it has a very vague notion of input encoding and such. Implementations are free to accept UTF-8 and the likes, but they're also free to reject them, so in practice if you want to be portable you have to use \uXXXX escape sequences, which of course defeats the point of Unicode identifiers (unless your IDE renders them as symbols). I don't know the state of Unicode support with g++, but MSVC will happily understand both \uXXXX and literal UTF-8 in identifiers, though for the latter it needs to see the UTF-8 BOM at the beginning of the file.
-
Re:I'd be happy if I could just use variable names
Just went ahead and tried Python again. Doesn't work.
Are you using Python 2.x? That's ASCII only. Python 3k is Unicode.
Think I also tried Javascript recently and it was broken, too.
That would be a non-conforming implementation of the language - per spec, it should permit "any character in the Unicode categories: “Uppercase letter (Lu)”, “Lowercase letter (Ll)”, “Titlecase letter (Lt)”, “Modifier letter (Lm)”, “Other letter (Lo)”, or “Letter number (Nl)”.".
And I know C++ complains, at least with g++.
C++ is kinda funny. It permits Unicode identifiers as of C++11, but it has a very vague notion of input encoding and such. Implementations are free to accept UTF-8 and the likes, but they're also free to reject them, so in practice if you want to be portable you have to use \uXXXX escape sequences, which of course defeats the point of Unicode identifiers (unless your IDE renders them as symbols). I don't know the state of Unicode support with g++, but MSVC will happily understand both \uXXXX and literal UTF-8 in identifiers, though for the latter it needs to see the UTF-8 BOM at the beginning of the file.
-
Re:I'd be happy if I could just use variable names
Just went ahead and tried Python again. Doesn't work.
#!/usr/bin/python
# coding: utf-8ð = 3
print ðFile "./test.py", line 3
SyntaxError: Non-ASCII character '\xc3' in file ./test.py on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for detailsOkay, fine, let's add a coding statement:
#!/usr/bin/python
# coding: utf-8ð = 3
print ðFile "./test.py", line 4
ð = 3
^
SyntaxError: invalid syntaxThink I also tried Javascript recently and it was broken, too. And I know C++ complains, at least with g++.
-
Re:So it's like Python 3That would be nice, but Python unfortunately doesn't do that. As I posted above, you could still do the same from Python, though. You could check sys.version_info to see if it's the desired version, and if not use Python's built-in Windows registry library to find the right version.
Sidenote: if you wanted it to work with Python 2 and 3 without using 2to3, use this: (sorry, it doesn't want to indent properly, so you'll need to add indentation as needed)try:
import _winreg as winreg
except:
import winregYou can then get the install path for the version of Python you want (winreg.QueryValue(winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\\Python\\PythonCore\\2.7\\InstallPath'), replacing 2.7 with whatever version you want), and run it with your script (sys.argv[0]) as the argument.
-
Re:EXE wrappers and third-party libraries
That depends. Is there an automated tool to create and deploy such EXE wrappers yet?
Not that I'm aware of, probably because it's about five lines of C#. It would also be doable fairly easily from Python as well, but it seems kludgy to make a Python script to find the proper Python runtime then call it on another Python script. Read HKLM\SOFTWARE\Python\PythonCore\$MAJOR.$MINOR\InstallPath (or HKLM\SOFTWARE\Wow6432Node\Python\PythonCore\$MAJOR.$MINOR\InstallPath on 64-bit Windows if you want 32-bit Python), if the key doesn't exist then display an error (preferably with a link to download the proper version), otherwise run $InstallPath\python.exe $SCRIPT.py.
That being said, again, most Python programs will ship with their own Python runtime (with py2exe or similar) if they depend on a specific version like that. Additionally, if you have users with enough smarts to be able to figure out installing Python on their own and don't expect them to change the installation location (or this script is just for yourself), you can obviously use a one-liner BAT file, but I wouldn't expect to see that outside of closed deployments.
I'll agree it's not as simple as it is on Linux distros (although some use python27, and some use python2.7, for instance), but it's certainly not difficult.And harder when your program relies on third-party libraries that have not yet been ported to 3.
This I fully agree with, and is unfortunately true. However, most libraries on PyPI have been ported to Py3k these days (at least ones with ongoing development). A list of PyPI packages that haven't been ported to Py3k yet can be found here. You could (most likely) easily run 2to3 on other libraries yourself, and fix whatever minor problems arise. It would be harder in libraries with C extensions, but it's almost always trivial for pure Python libraries.
-
Re:So it's like Python 3
some Python environments (such as Windows) can't easily have multiple versions installed side-by-side
The Python installers on Windows install to (by default) C:\Python$MAJOR$MINOR (Python 2.7 installs to C:\Python27, Python 3.2 installs to C:\Python32). For scripts that require a certain version of Python, you may have to open it with the proper version yourself, because (obviously)
.py files can only be associated with one program at a time. But for scripts made to be run by end-users, (assuming they don't ship their own Python runtime in the first place) a simple EXE wrapper can use the right interpreter. What part of this isn't easy?
Also, many developers make things compatible with both, which is made much easier with 2to3. Quite a few setup.py scripts use 2to3 automatically even, to be compatible with both using the normal python setup.py install. -
Re:The nice thing about Python
The fact that it looks nice is highly relevant, it means the human reader and the compiler see the same thing. That results in fewer errors and less visual noise.
I always look with amazement at the discussions on tab vs space vs some complicated mixture of both when people program in languages languages that ignore whitespace in the first place. If the block delimiters are so wonderful why make such a fuss about the indentation? Just read the way the compiler reads it and you can't go wrong. Oh, you mean that humans are better at reading indented code? Then why not make a compiler that reads it like a human would? I think that makes a lot of sense, and that is one of the reasons I like Python.
With Python it's considered a mistake now that they allowed both tabs and spaces for indentation, it should have been just spaces and that has been the recommended practice for many years. Perhaps the problem arises from people continuing those stupid wars instead of agreeing with the recommendation, and not from the idea that it's useful if humans and compilers read the code in the same way. If you work in an environment where this is an issue use the -t and -tt options, as the linked style guide recommends.
If your editor can't show the difference between tabs and spaces, can't easily insert or remove a '#' at the start of a block of lines, can't copy blocks of lines easily and can't change the indentation to what's appropriate for the destination, well, my advise would be to look for a better editor. You may be correct that Python is a pain in combination with an inadequate editor, but there are more powerful editors available. Use them.
-
Re:Really?
Agreed that PHP needs a major cleanup, but the resultant product probably shouldn't be called PHP 6
I agree entirely.. try some of these forks: http://www.ruby-lang.org/ http://www.python.org/ http://www.java.com/ http://www.microsoft.com/net, http://nodejs.org/
They are actually good for a change!
-
Shebangs on Windows
What you say works well in a UNIX-like environment. Windows, on the other hand, doesn't see #! lines; instead, it looks in a registry that maps the end of a filename to an application. It took until Python 3.3 for PEP 397 launcher to get implemented and added to Python.
-
lamitpobus si margorp ruoY
#TrollTalkComReversePsychologyKiller.py (Ver #2 by APK)
def reverse(s):
try:
trollstring = ""
for apksays in s:
trollstring = apksays + trollstring
except:
print("error/abend in reverse function")
return trollstring
s = ""
print reverse(s)try:
s = "Insert whatever 'trollspeak/trolllanguage' gibberish occurs here..."
s = reverse(s)
print(s)
except Exception as e:print(e)
ok, so you suck at python, no problem, we'll help you through this:
* there has been a function called reversed() for ages in python, no need to rewrite your own inferior one
* you could have used slices. it's slower than reversed(), but much shorter and elegant: print(s[::-1])
* your code is suboptimal, the *.pyc needs to be "recompiled" whenever you edit *.py (i.e. every time). you better use raw_input() and prompt the user for new input every time
* here is a one-liner in command line doing the exact same thing: python -c "print ('hello troll')[::-1]"
* here is a one-liner in command line doing the exact same thing, unix style: echo 'hello troll' | rev
-
Re:Light Table - Why it's Cool
How is this informative? This guy clearly doesn't know what he is talking about. PyDev (Plugin for Eclipse) gives you autocomplete since ages. You don't need static typing to be able to read out interfaces. IPython (interactive python extension) does it too. Or like pretty single one of these http://wiki.python.org/moin/IntegratedDevelopmentEnvironments . Hell, there even a VIM autocompletion plugin, if you are into that sort of stuff.
As for differences, how about showing results of your program as you type? Or Method-based hierarchie instead of file-based one? Watch the intro video one more time, please.
-
Python with CherryPy and Django
I've been able to build some surprisingly sophisticated and full-featured web applications using Python wrapped inside CherryPy and sending the output through Django. Really amazing how much you can do with this, with the added benefit of portability if you ever want or need to move it across platforms.
If portability isn't an issue, by using Python's ctypes you can call almost any back-end Linux, Windows or Mac OS X library for the ability to do almost anything you want. -
Python with CherryPy and Django
I've been able to build some surprisingly sophisticated and full-featured web applications using Python wrapped inside CherryPy and sending the output through Django. Really amazing how much you can do with this, with the added benefit of portability if you ever want or need to move it across platforms.
If portability isn't an issue, by using Python's ctypes you can call almost any back-end Linux, Windows or Mac OS X library for the ability to do almost anything you want. -
Re:Obviousness
What's amusing is that the 9 lines in question don't even implement the algorithm; they perform a quick sanity check before the real computation starts. Is this really the best they have? Couldn't they have found more creative lines of code to be infringing on a copyright?
Anyway I've been looking some stuff up. TimSort was originally written into Python by Tim Peters in 2002 (BSD-style license). If I'm not mistaken, that would mean that Sun wrote a trivial check as part their own re-implementation of someone else's work, and are claiming massive copyright damages on it. If Oracle wins that, that's one hell of a precedent.
-
Re:Python
Surely not! A python name is just a label not an object in it's own right (everything in the python universe since py2.2 (?) being either an object or a name) A name cannot have it's own location (only objects do)
Why do you believe that only objects can have its own location? On the contrary, quite clearly a variable is a named location. An object by itself is not a location, though its slots (fields) are. In fact, an object in Python is basically just identity + type + slots (locations).
I'm not sure that I follow you here!? If you assign to a captured value, you will see the new value in that namespace precisely because assignment does rebind!
You missed a very important part of my original example - you need to declare the captured variable as "nonlocal" in bar and baz (for which you'll need Py3k). Python semantics are that, by default, if you assign to a variable that is not declared in current scope, it is then automatically declared in that scope (thereby hiding any outer declarations). This is what you observe, not name rebinding - every scope has its own name, bound to its own location. So after you have assigned to "a" inside bar, you are no longer working with the original variable - you're working with a new variable that is local to bar, and of course baz will not reflect any changes you make to it. Try it with "nonlocal", though, so that all three functions work with the same variable, and you'll see the effect I had described.
-
Re:Why do people ask questions like these?
If you're looking to learn something new and general purpose, Python has a combination of decent docs (you can start with http://www.python.org/doc/ , http://pleac.sourceforge.net/pleac_python/ , and http://www.lightbird.net/py-by-example/ ), good libraries (see http://pypi.python.org/pypi and https://github.com/languages/Python/most_watched ) and all-around flexibility (all the regular system stuff, lots of microframeworks for web, scientific computing tools, 2d+3d graphics).
You may want to take a look at IPython ( http://ipython.org/ ), Reinteract ( http://fishsoup.net/software/reinteract/ ), and DreamPie ( http://dreampie.sourceforge.net/ ) for some interactive shells/interpreters to play around with. I use vim for programming, but there are a number of IDEs. Of the ones I've tried, I thought IEP offered the most interesting tools: http://code.google.com/p/iep/
Probably the fastest/easiest way to learn (and learn if you like) Python is to go through Zed Shaw's book/exercises: http://learnpythonthehardway.org/
There's a lot of other stuff on the Python wiki: http://wiki.python.org/moin/BeginnersGuide/ProgrammersSlashdot definitely isn't what it used to be. For programming questions you may want to look at Stack Overflow or Quora. For general nerdly news, I find Hacker News, Techmeme, and The Verge tends to cover my bases better these days.
-
Re:Why do people ask questions like these?
If you're looking to learn something new and general purpose, Python has a combination of decent docs (you can start with http://www.python.org/doc/ , http://pleac.sourceforge.net/pleac_python/ , and http://www.lightbird.net/py-by-example/ ), good libraries (see http://pypi.python.org/pypi and https://github.com/languages/Python/most_watched ) and all-around flexibility (all the regular system stuff, lots of microframeworks for web, scientific computing tools, 2d+3d graphics).
You may want to take a look at IPython ( http://ipython.org/ ), Reinteract ( http://fishsoup.net/software/reinteract/ ), and DreamPie ( http://dreampie.sourceforge.net/ ) for some interactive shells/interpreters to play around with. I use vim for programming, but there are a number of IDEs. Of the ones I've tried, I thought IEP offered the most interesting tools: http://code.google.com/p/iep/
Probably the fastest/easiest way to learn (and learn if you like) Python is to go through Zed Shaw's book/exercises: http://learnpythonthehardway.org/
There's a lot of other stuff on the Python wiki: http://wiki.python.org/moin/BeginnersGuide/ProgrammersSlashdot definitely isn't what it used to be. For programming questions you may want to look at Stack Overflow or Quora. For general nerdly news, I find Hacker News, Techmeme, and The Verge tends to cover my bases better these days.
-
Re:Why do people ask questions like these?
If you're looking to learn something new and general purpose, Python has a combination of decent docs (you can start with http://www.python.org/doc/ , http://pleac.sourceforge.net/pleac_python/ , and http://www.lightbird.net/py-by-example/ ), good libraries (see http://pypi.python.org/pypi and https://github.com/languages/Python/most_watched ) and all-around flexibility (all the regular system stuff, lots of microframeworks for web, scientific computing tools, 2d+3d graphics).
You may want to take a look at IPython ( http://ipython.org/ ), Reinteract ( http://fishsoup.net/software/reinteract/ ), and DreamPie ( http://dreampie.sourceforge.net/ ) for some interactive shells/interpreters to play around with. I use vim for programming, but there are a number of IDEs. Of the ones I've tried, I thought IEP offered the most interesting tools: http://code.google.com/p/iep/
Probably the fastest/easiest way to learn (and learn if you like) Python is to go through Zed Shaw's book/exercises: http://learnpythonthehardway.org/
There's a lot of other stuff on the Python wiki: http://wiki.python.org/moin/BeginnersGuide/ProgrammersSlashdot definitely isn't what it used to be. For programming questions you may want to look at Stack Overflow or Quora. For general nerdly news, I find Hacker News, Techmeme, and The Verge tends to cover my bases better these days.
-
Re:You shouldn't. Nobody should.
Of course it's error-prone, but how else can you avoid SQL injection in any language?
Most languages support prepared statements that properly handle strings for you. Take a look at the python API for databases (this is sqlite3, but other dbs use the same system).
Straight from that page:
t = (symbol,)
c.execute('SELECT * FROM stocks WHERE symbol=?', t)
Notice the lack of escape_my_strings_no_really_please_this_is_the_right_method("string");. Clean huh? People still using sprintf() or string concatenation for this sort of thing after all these years reap what they sow.
As for your post below:
Even if SQL wasn't an issue, you still have to sanitize other things like shell commands.
The fact that you're even contemplating on running shell commands based on user input is pretty much damning in my opinion.
I guess you're the type of person they sell those SQL-injection-protection proxies to.
-
Re:9 lines were copied
Not really, the 9 lines in question were not a direct port of any of the code in the python implementation, but rather a simple range check to ensure that accesing a range of items in an array was a valid action prior to starting the sort. As the python operation worked on lists or list slices (which come with embedded pre-checked range data) rather than array ranges, it did not need to perform this action.
-
Re:Nobody likes PHP
The problem seems like everyone wants to knock PHP but not provide an alternative that's somewhat easy to use. I'm a self taught programmer starting with Color Computer BASIC back in the early 80's. I've programmed in several different types of BASIC and then C. I tried C++ but didn't like it, although I liked the ideas. I've also done lots of scripting and Perl. Now I'm doing pretty well with PHP scripting having done a bunch of personal type coding over the past 4 years or so. I did poke at python 15 years ago but was working a lot with perl and they seemed to be two camps; perl or python. And since perl was on all our systems, that's about as far as I got.
I found this: http://wiki.python.org/moin/PythonVsPhp
But the comparisons really don't give me a good reason to go to python, heck some of them are even reasons not to move such as it's a general scripting language (like perl) vs a web specific one and I need to use modules to write web code (same as perl).
So, is there another language I should be looking at? If I'm going to go with a general purpose type scripting language, why not perl which I'm already pretty familiar with?
[John]
-
doctest
python doctest ftw! - http://docs.python.org/py3k/library/doctest.html
-
Re:And for the reading
I know two teenagers who went through the official python tutorial and started their way toward being great programmers:
-
Python or Scheme
A lot of people these days recommend Python as a first language. It is easy to learn and powerful. I don't know which of them is best, but here's a list of Python tutorials aimed at non-programmers.
Another interesting choice might be Scheme. There are two very good books that use Scheme to teach programming:
- How to Design Programs is aimed at roughly your son's age group.
- Structure and Interpretation of Computer Programs is aimed at university students and assumes some basic knowledge of calculus (which I'm guessing is over your son's head, but maybe not!)
DrRacket is a good programming environment to use with either of them.
-
Re:Guido's wrong.
It takes more skill and system-programming knowledge to deal with the tricky interfaces between the internals of a Python interpreter and an external C++ program.
Is this experience talking, or guesswork?
Admittedly, I haven't had a need for it myself, but it looks easy enough. And you have plenty of options, too!
1. Extending Python with C or C++
2. ctypes
3. Cython
-
Re:Guido's wrong.
It takes more skill and system-programming knowledge to deal with the tricky interfaces between the internals of a Python interpreter and an external C++ program.
Is this experience talking, or guesswork?
Admittedly, I haven't had a need for it myself, but it looks easy enough. And you have plenty of options, too!
1. Extending Python with C or C++
2. ctypes
3. Cython
-
Re:Static vs. Dynamic Typing
Static typing can be shoehorned on Python by an external tool - it already has everything necessary in the syntax to provide type annotations, you just need something to actually enforce them. E.g. you can write, today:
def foo(x: int, y: int) -> int:
return x+y;but it doesn't really mean anything special to Python itself. It will stuff those things up into __annotations__ property of the function, and so a decorator can be written that wraps the function and verifies types at run-time. For compile-time, you need a tool.
-
Re:It's still too slow, despite what he says.
Unfortunately PyPy only supports the legacy Python 2 dialect, not Python 3. Given that Python 2 is a dead end and all new code should be written in Python 3, this makes PyPy a non-starter for many people.
This is unfortunate, but not a total deal-breaker. The recommended compromise is to write in Python 2.x, but plan on using the 2to3 tool to convert to Python 3.x once you no longer need the Python 2.x support. And, you can run 2to3 as a check, to make sure you aren't writing any code that is too horribly tied to Python 2.x.
The 2to3 tool can port most Python 2.x code for you, so it isn't hard to avoid the hard-to-convert bits of Python 2.x. As long as you only use new-style classes, you use lazy iterators as much as possible (use xrange() instead of range(), etc.) and you don't do any deep magic with exceptions, Python 2.x code is a lot like Python 3.x code and the conversion is smooth.
http://wiki.python.org/moin/Python2orPython3
steveha
-
Re:It's still too slow, despite what he says.
Says the guy whose whole life is tied up in the language
That's fair.
and whose project, at Google, to speed it up, crashed and burned.
That's completely wrong. Unladen Swallow was not GvR's project, and it didn't "crash and burn". Unladen Swallow found that their approach was not speeding up Python as much as they had hoped, and the two Google employees were moved on to other projects. The code lives on, and I think people are still doing things with it, although it's clear that PyPy is a better approach.
To me at least, "crash and burn" implies a horrible failure with catastrophic consequences, and Unladen Swallow was considerably less dramatic than that. If you just meant to say "they didn't accomplish their goals", that would be a fair statement.
Python is slow because von Rossum refuses to cut loose the boat-anchor of "anything can change anything at any time".
That was a basic decision way back when, and it would be a profound change to make (the language wouldn't really be Python anymore). I personally don't make much use of this, but supposedly there are some programs that do.
PyPy, the newer Python implementation, uses two interpreters and a JIT compiler to try to handle the dynamism with less overhead. They're making progress, but they need a very complex implementation to do it, and they're many years behind schedule.
There is a very small group of people working on PyPy, and they are doing ambitious things. I'm not overly worried about their schedule.
You failed to mention that PyPy has already achieved great speed when compared to CPython. The latest PyPy is, on average, over 5x as fast as CPython.
if a module or class could only be modified from outside itself if it contained explicit self-modification code (like a relevant "setattr" call) most modules and classes could be nailed down as fixed, "slotted" objects at compile time.
This is an interesting idea. But I am not aware of anyone working on something like this.
Claiming that the "slow parts" should be rewritten in C is a cop-out.
I disagree. Way back at the dawn of time, GvR designed Python to make it easy to interface C code, just exactly as this sort of "escape hatch" to help projects that work well but are too slow, and also for libraries.
I think that the ability to link in C code was an important feature that helped Python win hearts and minds. It's slower than C, but at least you knew there is an escape hatch if you wind up having trouble with it.
Except for number-crunching, or glue code for existing libraries, it's seldom done.
Yeah, but that's kind of like saying that except for stop signs and traffic lights, you usually don't use the brake pedal in a car.
There are all sorts of useful libraries that have been glued into Python, and a major part of Python's popularity is that you can use powerful libraries from a convenient and friendly language.
SciPy is a great example: they took ferociously powerful libraries (written mostly in Fortran) and made them usable from Python. I know I for one can get more work done in Python than in Fortran.
As another example, a few years ago I worked on a project to make a DVD player with some fancy features, and we were doing our development in Python. It was fast enough, even on a cheap embedded processor, because all the heavy lifting was being done by C library code. And we got a lot of work done quickly.
(I have a Python program running right now which will run for over a week, parsing the street address of every business in the US into a standard format. The parser is complex enough that rewriting it in C would be a big job. There's no "inner loop".)
This sounds interesting.
Can you run this in PyPy?
Can you fan it out to multiple processes using the multiprocessing
-
Re:For you, maybe.
And one could say that Linux is good for Linux programming, otherwise not so much. I wouldn't do Windows programming on Linux, or Mac programming on Linux. Each platform is really best at programming for that platform. For the stuff that's truly cross-platform (in your examples, Python and JavaScript are like that, but not C++), the story is a lot nicer than you are pretending it to be, perhaps out of ignorance. For all the examples you gave, there are either Windows-only solutions, or cross-platform ones. Heck, you can install and use Vim on Windows if you want. That's what I do. Eclipse runs fine on Windows. All the other IDEs run on Windows (Qt Creator, Code::Blocks, Visual Studio; and for Python: http://wiki.python.org/moin/IntegratedDevelopmentEnvironments -- note that all of them run on Windows, but not all of them run on Mac or Linux). All the major webbrowsers run fine (for JavaScript). All of the open source text-editors work on Windows, and there is a plethora of Windows-only text-editors of varying quality that are available to you. If you insist on using the command line, you can always install cygwin (people bitch about it, but for what one would need a Unix shell on Windows for, it does just fine). At worst, in Windows you end up using open source or ported software. At best, you get access to stuff that doesn't exist on Linux and you get to use it on a platform that's more stable and doesn't require you to reboot if you want to play games or do true Windows development.
Here's a weird case that happened to me. I was trying out Haskell and I found the experience better on Windows than Linux. I mean, both were frustrating since Haskell isn't a mature platform, but on Windows installed Leksah and the Haskell Platform was a breeze. On Linux, there were some issues. I managed to fix many of them, but I still find that Leksah runs smoother on Windows than on Linux. So there's that...