English 中文(简体)
From a coder s perspective, what kind of project should I choose python over php for where both could do the job?
原标题:

I ve never used python before. I ve used php for about 5 years now. I plan to learn python, but I m not sure what for yet. If I can think of a project that might be better to do in python, I ll use that to learn it.

Edit: just to add this as an important note, I do mean strictly for linux, not multi-platform. Edit 2: I m hoping for objective answers, like a specific project, not a general field of projects, etc.

最佳回答

Python is better suited for practically anything that doesn t fall within PHP s specialty domain, which is building websites.

If you want a list of programming projects that you could work on, see this thread: https://stackoverflow.com/questions/1022738/i-need-a-good-programming-project

问题回答

PHP for websites. Python for pretty much anything else, such as commandline tools, long-running scripts, daemons, etcetera. If you re writing a PHP script and you re reaching for functions in the posix extenstion, shared memory or other low-level stuff then that s generally a sign that Python would be better suited. It s not that PHP can t do it, but Python just does it better and less buggy.

Especially when you re venturing into background daemons for your website you ll want to look at Python. PHP has some garbage collection problems in long running processes such as daemons. Also, some functionality is much easier and clearer in Python (e.g. redirecting STDIN, STDOUT and STDERR. PHP misses posix_dup2()). Also, Python has threads :-)

The only time when I now use PHP background daemons for my websites is when they can re-use significant amounts of code (such as with MVC frameworks like CakePHP).

One more advantage of Python is that there are many, many libraries for it, because it s rather easy to create a Python wrapper for a C library. So, Python has libraries that PHP doesn t have (OpenGL, multimedia, etcetera). So if you re into those areas Python becomes the obvious choice.

I am going to answer based purely on ease of programming and encouragement of good design practices.

I learned to program using Perl and for many years wrote web applications using CGI, which was never pleasant in the first place. I always found that Perl was tedious to get right and way too easy to get wrong.

When I started developing web applications more seriously, I discovered PHP. Going from Perl to PHP was a natural progression because they share many of the same methodologies and syntax. I loved that PHP was so heavily installed and widely used and was much easier to code in than Perl. Except, like Perl, PHP makes it way too easy to do things the wrong way, and ushered in a new era of poorly-written and insecure web applications.

Now... Four years ago I discovered Django, which was in its infancy at the time, and it changed my life. I used this as the motivation to learn Python and have not looked back, not once. I use Python for everything now:

  • System programming (server configurations, monitoring, alerting)
  • Network programming (router/switch configuration automation, auditing, syntax checking)
  • Web applications (self-explanatory)

Python s ease of use, elegant syntax, and the fact that it encourages best practices by default is what turned me on and kept me interested. Give Python a shot, you won t regret it!

A project you want to maintain over any length of time.

I ve had to maintain PHP code and there is something about the fact that you can mix HTML and code that makes PHP stuff a nightmare.

Python has a much higher level of abstraction and makes more maintainable code much easier to write and much more importantly for maintenance - read.

All IMHO of course as this is a rather subjective question.

I only use php for the following reasons:

  1. The software I m using that solves my problems is written in it (like Wordpress. Don t try to program everything, sometimes you have a lot of good stuff in php);
  2. I need/want to run in a lot of different server configurations (a lot of people use shared hosting, and can t install/don t have support to python, but php is a default in any web hosting company);

When I can choose (I have server environment control and I don t mind factor 2) , I choose Python. Just because you can write good code in php doesn t mean you should. I miss a robust language, with good Exception handling and a lot of other advantages. The language is clean and you can even use Google App Engine for free if you want to learn Python.

You can write almost everything you write in Python with PHP. But I dislike PHP because a lot of times you use a feature, and you know that you have to configurate your php.ini, or use an obscure function just after having a lot of headaches with the feature.

IMHO, at least in my experience, PHP is something you get used it. And Python is something you just fall in love: because it just works.

I know these last paragraphs may be a personal opinion, but the first two factors aren t. Just choose the language what you think it s worth in that case.

Anything that requires background processing or any significant amount of code that doesn t just show a user a page. Python is really good as a scripting language, and writing a command line Python script is commonplace; writing a PHP script to do command line work is rare.

My company was contracted to build a web application last year, and the client specified that it should be done in Flex. Now, this application should have been a web application, but we had a unique opportunity to try something new.

We had absolutely no idea what we were doing at the time, but it was a great learning experience. My advice would be to try something new when you get the chance, make mistakes, and continue to learn.

Might be harder if you want to learn Python casually... Try getting someone to pay you for using it.

If you re doing any multi threading development, pick Python over PHP.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签