PHP与Python的比较 (PHP vs Python)

来源:互联网 发布:ar增强现实源码 编辑:程序博客网 时间:2024/06/01 09:12



在这里,我节录了一些关于PHP和Python比较的文章和讨论,让我们能从另外一个 角度来看待PHP的优点和缺点。
同样作为一种脚本语言,底层同样是由C语言来开发的,Python是OO的,现在PHP也OO了。Python是多用途的(可用于桌面应用和web应用),PHP是专长于web应用的。当PHP还在苦恼如何整合进去大程序时,Python早就可与Java,。NET相连了。。。。

    * Python的可读性,可维护性比PHP好;
    * PHP的发展存在一个向后兼容性的问题,Python不存在;
    * PHP的入门门槛底,Python比较高(其实Python语言是挺简单的,难在要有OO的思想,PHP在OO化后,面临同样的问题);
    * 到目前为止,支持PHP的服务器多,支持Python的少 (随着硬件成本的降低,估计状况很快会改变)。


曾经有人说过,PHP的底层架构不适合用OO,那Python为什么OO用的那么好?
曾经有人说过,PHP用OO速度慢,那用Python的人,为什么并不觉得慢?

PHP用OO个人觉得是件好事,但看过Python以后,感觉PHP现在实现OO的功能与方法比Python还多,是好还是坏?作为一种脚本语言是否能很好的承载得起呢?

感觉PHP有点像当年的C++,既要面向OO,又要兼容C,搞得倍复杂。而Python有点像当年的Java,从开始就是面向OO的,并立足于脚本语言,对OO的功能与方法作了相当的简化,做自己力所能做的事情。

PHP以简单易学而名,现在为了企业化应用,如果搞得像C++一样既要顾及兼容性,又几乎全盘copy Java的OO功能,难免会变得复杂,之后会不会被其他语言所代替呢?

他山之石,可以攻玉。我们是否应该学习一下Python,从中得到一些启发呢?

现在当大家都一致看好PHP时,结合种种迹绍,我似乎感到了背后的一丝寒风,。。。希望只是己人忧天。

从PHP发展的角度,为今之计,我想最好的办法,就是大力普及推广PHP在中国的应用,先占领市场再说,因此写了个建议 (连接),可惜看来也是胎死腹中了。。。

==========================================================================
第一部分:介绍Python
What is Python?

(PYTHON的中文解释 :http://www.okpython.com/bbs/thread-73-1-1.html)

Python is a programming language that is freely available and that makes solving a computer problem almost as easy as writing out one's thoughts about the solution. It can be written once and run on almost any computer without needing to change the program. In this section, you can learn more about what Python is, how it is used, and how it compares to other programming languages. Use the menu below to browse the various sections of this article.
Python was first created by Guido von Rossum in 1990 and was named after the Brit-com Monty Python's Flying Circus. It has since been developed by a large team of volunteers and is freely available from the Python Software Foundation. The most recent version of Python, 2.5, was released in September, 2006.

Python is now found in many incarnations. At the website of the Python Software Foundation, Python is written in C. Other incarnations exist. A Java-based version of Python exists in Jython and may be used to work with Java code natively.

Iron Python, a C# version, exists for the .Net and Mono platforms and allows C# programmers access to Python's power and flexibility. In each of these instances, Python is written in one language and works natively with that language; however, it also interacts very well with other languages through its many modules. For purposes of research and development, there is also a Python implementation written in Python itself. The project PyPy was founded in 2003 in order to enable Python programmers to change the behaviour of the Python interpreter at will. While it is an open source project, being developed openly by a community of developers for free distribution and modification.
How Python is Used

Python is a general purpose programming language that is able to be used on any modern computer operating system. It may easily be used for processing text, numbers, images, scientific data, or anything else which one might save on a computer. It is used daily in the operations of the Google search engine, the video sharing web site YouTube, NASA , and the New York Stock Exchange. These are but a few of the places where Python plays important roles in the success of business, government, and non-profit organisations; there are many others.

Python is also an interpreted language.

This means that it is not converted to computer-readable code before the program is run but at runtime. In days gone by, this type of language was called a scrīpting language, intimating its use for trivial or banal tasks. However, programming languages such as Python have forced a change in that nomenclature. Increasingly, large applications are written almost exclusively in Python. As mentioned above, in addition to being used by Google and NASA to complement other languages, Python is used almost exclusively for such applications as YouTube and the web-based transaction system of the New York Stock Exchance (NYSE).
How Does Python Compare to PHP?

As it is another programming language, the commands and syntax of Python differ from other interpreted languages. PHP is increasingly displacing Perl as the lingua franca of down-and-dirty web development. However, more than either PHP or Perl, Python is much easier to read and to follow.

At least one downside which PHP shares with Perl, however, is its squirrely code. Because of the syntax of PHP and Perl, it is much harder to code programs that exceed 50 or 100 lines. Python, on the other hand, has readability hard-wired into the very fabric of the language.

The readability of Python makes such programs easier to maintain and extend, as well.

While it is starting to see more general usage, PHP is, at heart, a web-oriented programming language. The name itself stands for "PHP: Hypertext Processor", suggesting the fact that it was meant to output web-readable information, not handle system-level tasks. This difference is exemplified in the fact that one can develop a web server in Python that can understand PHP (e.g., Zope), but one cannot develop a web server in PHP that can understand Python.

Finally, Python is object-oriented. PHP is not. This has significant implications for the readability, maintability, and scalability of programs written in each.

(source: http://python.about.com/od/gettingstarted/ss/whatispython_4.htm)

===================================================================================
第二部分:比较讨论 (这是在Python论坛的讨论,比较激烈)
Python v PHP: fair comparison?

Original post:

- Python is more readable, and more general purpose
- PHP has awful backward compatibility
- PHP has a lower barrier to entry
- Most inexpensive web-hosters support PHP, but not Python
- PHP has far more pre-writen scrīpts available
- Newer versions of mod_python require Apache 2.0, which few hosters have
- There is more demand for PHP developers, than Python developers

(source: http://mail.python.org/pipermail/python-list/2006-November/413927.html)

===========================================

Responsed post: (择录)
----------------
For the most part you wouldn't ever thing about writing data
conversion programs, GUI applications, Windows services, COM+ objects,
Linux daemons, simple scrīpts, socket server/client applications,
etc. in PHP (but all can be done in Python).  Web apps are just one of
the many applications that Python can tackle.  PHP is normally
relegated to dynamic web page creation and database interfacing of
a web application.  I've never seen it promoted as a language
that can be used to develop a standalone non-web browser based
application (but I'm sure someone will tell me that it can be used
that way ;-).

I agree that you can use PHP to do some basic Web apps more
quickly and with more general availability in PHP than Python.
I think there is a reason that Java and Python are used to power
the larger application frameworks that I see (e.g. WebSphere,
WebLogic, Zope, etc.).

I find many hosting companies that have mod_python now (this
wasn't the case a couple of years ago).  Hosting costs have
dropped so much during that time that getting Python doesn't
cost much (if any) extra.

I'd be surprised if there was more demand for PHP developers
than Python developers.  Google lists 51 PHP jobs and 168
Python jobs in their internal jobs database (I just did a
quick search).
-------------------------------------------
I don't think so. Python has more intuitive syntax for beginners and is one of
the best choices for the first programming language to pick up.
---------------------------------------------
> - Python is more readable, and more general purpose

Yes, php is only for web.
On the other hand, Python is a general purpose language and it can be
used for nearly anything you may want to do.

> - PHP has awful backward compatibility

Yes. And it's also an ugly language to work with.

> - PHP has a lower barrier to entry

I don't think it is any easier than python. Not even to begin with.

> - Most inexpensive web-hosters support PHP, but not Python

Sad but true.

> - PHP has far more pre-writen scrīpts available

For web projects, perhaps.
But I'm sure you can do everything better with python, especially with
the new crop of web frameworks (Django, Turbo Gears, etc...).

> - Newer versions of mod_python require Apache 2.0, which few hosters
> have

You can also get alder versions of mod_python. What's the problem?

> - There is more demand for PHP developers, than Python developers

So you want to be a web developer?
Then look no further. Learn python and go kick php developers asses in
the market place.
There are thousands of php developers out there. Do you want to be just
one more?
I'd rather learn something newer, and much more powerful.
And once you get a job, you will do everything better and faster than
the others, your quality will stand up from the rest and so your
reputation.

------------------------------------------
Absolutely false.  Most of my standalone, command-line scrīpts for
manipulating my unix users in LDAP are written in PHP, although we're
rewriting them in python.

Although I can't think of a single app written in php that's not web-
based (other than standalone scrīpts I have written), there are up-to-
date php bindings for GTK: http://gtk.php.net/
----------------------------------------------------
Fair enough, this has been possible due to the enormous popularity of php.
You can also hit a nail with your shoe instead of using a hammer, and if you
try hard you may even succeed. You can also digg a a grave with a spoon
instead of a shovel, and after a couple of days you'll be finished.
You may be able to use php to create a stand alone app, but php was created
with the solely purpose of being a tool for creating dynamic web sites.
And you have to admit that using php for a non-web app is like trying to fit
a a square into a round hole.
Php is no more than a simplified C-ish language, dynamically typed with a
lot of functions aimed at web tasks. Without these built-in functions,
there's little you can do with it in your web apps.
On the otehr hand, you don't need a thousand built-in functions in python
for achieving every single task on a web site. You just write them, often
with just a couple of lines, because the language is that flexible an
consice.
You don't have to remember each and every function for every task, this is
ridiculuous.

I think that even as a general purpose language, python is still much more
adecuate, fast, fun and flexible than php for building web apps.

===================================================================================

第三部分:讨论二 (这是在中性论坛里的讨论,比较温和)

original post:

Can Python replace PHP?
Can I use a python program to make an interface to a mysql 4.X database?
If that's possible where can I find a tutorial?

(source: http://www.thescrīpts.com/forum/thread19621.html)

-----------------------------------------------

Responsed posts: (择录)

--------------------

The short answer is "yes" to both questions, but with a caveat: Python is
a general-purpose programming language, where PHP is designed to write
web applications. So it's not quite as simple/straightforward to use
Python the same way you use PHP for plain web pages; conversely, if
you've been running into difficulty with PHP in trying to write
complicated program logic, you'll find Python a big help.

-------------------------

。。。。

Spyce is the closest thing to PHP, but also provides some features PHP
does not. However, I think you will find some people, like myself, who are
still unable to make the switch from PHP to Python for web development,
because of a lot of little things. These people know Python is better and
may even know their non-web PHP applications are hell compared to their
Python re-implementations. However, they still use PHP for web development
and feel they have to. They may have even written out all the requirements
and received vague responses to them.

Python web development is fragmented. Mod_python CVS now contains the work
of what used to be mod_psp by Sterling Hughes, a PHP warlord, I'm sure
he'll reply. This possibly soon to be official 'PSP' is not much in my
opinion and should probably not be folded into mod_python. Others agree:

http://www.modpython.org/pipermail/...une/003200.html

Another big problem with Python web development is the lack of good shared
hosting support. Primarily the lack of anything resembling PHP's safe
mode. Mod_python has no equivalent of this safe mode, but Apache 2's
'Perchild MPM' is supposed deprecates this need. However, this still
requires an httpd.conf edit and a restart of Apache for every user added,
very annoying, especially to a shared hosting service.

-------------------------

Python is more beautiful and more powerful, but for this purpose I
consider the current implementations still lacking compared to PHP.
So much so that I am unable to bring myself to write new PHP, but
unable to continue using Python until I find the perfect implementation.

--------------------------

Since PHP is widely adopted as a web programming language, I assume
you think PHP's installation methods are robusts and general?
Let me assure you, having done PHP configuration in Apache, it
is far from it -- in fact, it is buggy and annoying beyond all
measure. [I was *shocked* to discover, for example, I can't associate
all .html files in a directory with PHP. I am sure it is a great
visibility booster for PHP to have pages end with .php, but I didn't
care much for it.]

I actually think that in a world where people think PHP installation
methods are sane, Python's barrier to entry (in that regard) is fairly low.

----------------------------

PHP's installation is difficult, but fairly robust. The result is a
system where pages can be added easily, multiple users can coexist,
server health is generally maintained, and administrator overhead
*after* installation is pretty good. For a large set of applications,
you don't have to interact with the administrator during installation or
development of your application. Interacting with system administrators
usually does not make developers happy, so that's a big plus.

PHP certainly sucks in a whole bunch of ways, installation included, but
it gives administrators what they want (simplicity and delegation), and
gives users what they want (an available environment with a relatively
decent learning/usage curve).

--------------------------

[color=blue]
> On top on this there are severeal modules for templating etc. PHP does
> not have these modules, so everybody has to do it in PHP's own faulty
> way.[/color]

Nonsense, you're trying to spin a deficiency as an advantage. PHP is
its own template system, for better or worse. Python doesn't include
any template system and you're left on your own to find or write one,
install it, and get it working.
[color=blue]
> Python is not fragmented, but there are more high level modules for
> doing things.[/color]

Nonsense, of course Python (for web development) is fragemented, look
at all these different Python web modules we've been discussing.
Python does have a lot of modules (like Numeric) that PHP doesn't, but
they're not that useful for web programming. PHP is narrowly focused
on web programming and is far superior to Python for that purpose
unless you spend a lot of time enhancing Python with add-ons before
you even start writing your applications.
[color=blue]
> There are more PHP developers, that's right, but they are all starting
> from scratch, with a poor language. So it's their loss.[/color]

Nonsense, Python is a better language than PHP, but that's the only
advantage Python has for web development. The language is a small
part of the overall system. And it's Python developers who must start
from scratch, not PHP developers. Python could catch up by adding
more stuff to the standard library, but it hasn't done that yet and
most of the suitable Python modules for PHP-like usage aren't really
ready for prime time.

====================================
python vs php for web programming

(source: http://www.thescrīpts.com/forum/thread26027.html)

(想要了解更多Python知识的,可参看中文手册:http://doc.chinahtml.com/Manual/Python/tut/)
 
原创粉丝点击