Python vs PHP

S

Shufen

Hi all,


Can someone who has use PHP before and know quite well about the
language, tell me what are the stuffs that Python offers and PHP
doesn't. A few examples will be nice. I know about the date format
problem which PHP is having but I need more examples. Thank you for
any help.

Shufen
 
W

Wilk

Hi all,


Can someone who has use PHP before and know quite well about the
language, tell me what are the stuffs that Python offers and PHP
doesn't. A few examples will be nice. I know about the date format
problem which PHP is having but I need more examples. Thank you for
any help.

imho php is more to compare with template engines (like cheetah...)
than python himself.
 
C

Cliff Wells

Hi all,


Can someone who has use PHP before and know quite well about the
language, tell me what are the stuffs that Python offers and PHP
doesn't. A few examples will be nice. I know about the date format
problem which PHP is having but I need more examples. Thank you for
any help.

PHP and Python are both very dynamic languages. I've used both pretty
heavily (although Python far more) and I'll make these observations:

On paper, PHP is pretty good. In real life, the interpreter is badly
broken in many ways. Much of this is supposedly fixed in PHP5 with the
new Zend engine, but that remains to be seen. The object model in PHP4
barely works. It doesn't support chained references (i.e. a->b->c
doesn't work) or doing things like utilizing the return value of an
expression or function call unless it's been assigned to a temporary
variable (i.e. a function foo() returning an array can't be used thusly
foo()[0]. Whether or not this is a bad thing is arguable, I'm merely
demonstrating the language's lack of a feature, or rather, lack of
generality).

PHP has some excellent libraries for web development. In many ways this
makes up for the shortcomings in the language itself, since you can
quite often keep actual programming to a minumum. I've seen nothing in
the Python world that compares to Smarty templates (and yes, I've
reviewed the many Python templating engines available. To date I've
seen none that approach Smarty in actual
usability/readability/simplicity).

Outside of web development PHP makes little sense. One of the silliest
abominations I've ever encountered is PL/PHP, a procedural language for
PostgreSQL. The only thing arguably worse is PL/Perl. (Note: Luckily we
have PL/Python available <wink>).

Python, on the other hand, is a remarkable general purpose language (of
course you shouldn't be surprised to hear that here). If PHP is the
best language for the web (due to its tight integration with the server
and its specialized libaries), then Python is the best language for
everything else, and a close second for best language for the web (and
I'm sure you'll hear arguments that it's the best in that category as
well).

If you are strictly doing web development and are familiar with PHP,
then I feel you have nothing to lose (aside from gaining some broader
experience) with sticking with PHP. If you plan to do anything else,
don't even consider it. Use Python or some other general-purpose
language.

Regards,
Cliff
 
M

Max M

Cliff said:
PHP has some excellent libraries for web development. In many ways this
makes up for the shortcomings in the language itself, since you can
quite often keep actual programming to a minumum. I've seen nothing in
the Python world that compares to Smarty templates (and yes, I've
reviewed the many Python templating engines available. To date I've
seen none that approach Smarty in actual
usability/readability/simplicity).


Have you tried PageTemplates from Zope?

They are very clever, and the macros makes it much better at reusing
code than anything I have seen elsewhere.


--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
 
I

Istvan Albert

Max M wrote:

Have you tried PageTemplates from Zope?

They are very clever, and the macros makes it much better at reusing
code than anything I have seen elsewhere.

I second that. But the truth is that I really like
templating systems that are valid HTML, thus
can be manipulated in an html editor

Istvan.
 
M

Max M

Istvan said:
Max M wrote:



I second that. But the truth is that I really like
templating systems that are valid HTML, thus
can be manipulated in an html editor

Wich is exactly what PageTemplates are...

Most other systems use "illegal" html syntax for things like loops and
logic.


--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
 
C

Cliff Wells

Have you tried PageTemplates from Zope?

They are very clever, and the macros makes it much better at reusing
code than anything I have seen elsewhere.

Can they be used outside of Zope? One of my criteria was usability
<wink>
 
I

Ian Bicking

Cliff said:
Can they be used outside of Zope? One of my criteria was usability
<wink>

Yep, I use them regularly. zpt.sf.net has a somewhat out of date
distribution (it still works fine though), and simpletal is another
implementation. There's even a Perl implementation (PETAL). At some
point the Zope people are going to make a fully up-to-date release of
that package (without the rest of Zope), but they are mostly stuck on
the packaging issues (the dependencies have already been worked out; it
doesn't depend on the rest of Zope).
 
I

Ian Bicking

Shufen said:
Can someone who has use PHP before and know quite well about the
language, tell me what are the stuffs that Python offers and PHP
doesn't. A few examples will be nice. I know about the date format
problem which PHP is having but I need more examples. Thank you for
any help.

Well, in general I'd say Python is better than PHP at pretty much
everything but:
* Regularly available on commercial hosts
* Unified basis for web development (there's still a lot of MVC-like
frameworks built on PHP, but they share more in common than in Python)
* Always uses a multiprocess model that both protects against bugs (or
at least, your server stays up no matter how buggy your PHP app or
libraries), and not too inefficient (like CGI).
* Easy to install and administer PHP applications; there can be a lot of
nuisances when installing Python web applications, like sys.path etc.

When it comes to all the more general things, Python beats PHP on pretty
much everything. Python has a clear and general object model -- PHP is
just starting to add some more generic things to its object model, like
operator overloading, and computed attributes. It still falls way short
of even the earliest versions of Python. I'll only briefly mention the
amateurish object implementation that requires assigning intermediate
objects to variables; I don't even understand what hackery caused that
misfeature.

Python doesn't have references (except that everything is implicitly a
reference), which is 100% a feature -- references are a horrible idea.
PHP seems to be moving away from references in version 5 as well.
Everything in Python is call-by-reference.

Python has namespaces and good modularity.

Python doesn't muck up quoting. PHP's magic quotes are bizarre and
dangerous, IMHO. They were a way of making up for PHP's sloppiness in
other areas. It never folds things into the namespace like PHP --
again, PHP programmers are moving away from this.

Well, that's just a short list. Just about everything you can think of,
Python is arguably better than PHP. Except for the web programming
environments -- which have their benefits in Python, but it forces you
to make a lot of choices and compromises that would be better forgotten.
But we're finally making some progress in Python to fix that
situation. (Incidentally, I work full time doing Python web
programming, and I've done a fair amount of PHP programming in the past
as well.)

At the same time, there's still a lot of really good PHP applications,
which is a testament to the language. Any language where people can do
good work is a good language; and while PHP's advantages over Python
seem small, I think the evidence shows that they are quite significant.
 
A

Alex Martelli

Istvan Albert said:
I second that. But the truth is that I really like
templating systems that are valid HTML, thus
can be manipulated in an html editor

Me too. nevow forever...!!!


Alex
 
I

Istvan Albert

Alex said:
Me too. nevow forever...!!!

Hm, it says on their page:

"However, no actual code may be embedded in the HTML template"

alas these are the sort of design decisions that
won't adapt well for real life scenarios.

Should we use code in templates? No. Should we make
every possible effort to prepare the data exactly as
we need. Absolutely.

Yet as soon as the whole thing is working we realize
that we forgot something and that fixing it the
"right way" will cause a significant setback. That's
reality, trying to avoid these by mandating a
certain behavior will only make the framework less
usable.

And that is where ZPT shines. You want full separation
go ahead. You want to bend the rules a bit go ahead.
You want to do it the wrong way and put your entire
program into the first "define" tag, well, guess
what you can do that too.

Where ZPT sucks is in its messy way to deal with
expressions. There are whole bunch of ways to create
them:

* path expressions - locate a value by its path.
* exists expressions - test whether a path is valid.
* nocall expressions - locate an object by its path.
* not expressions - negate an expression
* string expressions - format a string
* python expressions - execute a Python expression

and when one starts combining these all kinds of
peculiarities will have to be dealt with.

Istvan.

ps. as I look that the data/render example, I noticed
the color attribute is actually created in the
code ... IMO this is exactly what we are trying to avoid
with templating. There is no indication in the
template that a new attribute is going to be added
to the tag ...

compare this to the same thing in ZPT:

<span tal:content="name" tal:attributes="style color">Tag Name</span>
 
J

JZ

Dnia Fri, 22 Oct 2004 03:56:44 -0700, Cliff Wells napisa³(a):
I've seen nothing in the Python world that compares to Smarty templates

I am using Smarty and I know them very well, but compiled Cheetah Templates
(http://www.cheetahtemplates.org) with its inheritance approach
is much powerfull and easier to use than Smarty. Cheetah can cache any
indyvidual placeholder or portions of template. Smarty caches only the
entire template output as a unit. Cheetah can be extended using inheritance
approach. Smarty can use only the containment aproach. Cheetah uses Python
with its all benefits. Smarty uses PHP with its messing function names, no
import idea, no namespaces idea, worse unicode library, no docstring, no
good free IDE/RAD editors etc.
 
J

JZ

Dnia Fri, 22 Oct 2004 03:56:44 -0700, Cliff Wells napisa³(a):
I've seen nothing in the Python world that compares to Smarty templates

I am using Smarty and I know them very well, but compiled Cheetah Templates
(http://www.cheetahtemplates.org) with its inheritance approach
is much powerfull and easier to use than Smarty. Cheetah can cache any
indyvidual placeholder or portions of template. Smarty caches only the
entire template output as a unit. Cheetah can be extended using inheritance
approach. Smarty can use only the containment aproach. Cheetah uses Python
with its all benefits. Smarty uses PHP with its messing function names, no
import idea, no namespaces idea, worse unicode library, no docstring, no
good free IDE/RAD editors etc.
 
A

Alex Martelli

Istvan Albert said:
Hm, it says on their page:

"However, no actual code may be embedded in the HTML template"

alas these are the sort of design decisions that
won't adapt well for real life scenarios.

Works fine for me.
Should we use code in templates? No. Should we make
every possible effort to prepare the data exactly as
we need. Absolutely.

The template is not going to be exactly the HTML code we need to output,
of course; that's what the nevow namespace is all about --
letting the template get the data it needs and pick the rendering
approach it prefers among those the application makes available.

Yet as soon as the whole thing is working we realize
that we forgot something and that fixing it the
"right way" will cause a significant setback. That's
reality, trying to avoid these by mandating a
certain behavior will only make the framework less
usable.

What setback? If you've omitted to provide (e.g.) a rendering method
you realize you need, you add it to the Python code and access it with
the appropriate nevow:data="whatever" attribute in the template. No
problem.

And that is where ZPT shines. You want full separation
go ahead. You want to bend the rules a bit go ahead.

Special cases are not special enough to break the rules.

That's what makes Python great. It's what will make nevow great when it
gets a bit more stable (it IS still at 0.3, after all -- and I've seen
bugs in 0.3 that are already fixed in CVS [duplicating the patch I was
going to submit, natch;-)]).
You want to do it the wrong way and put your entire
program into the first "define" tag, well, guess
what you can do that too.

If you think that's a plus, go ahead, but then to be consistent you
should be coding in Perl...

ps. as I look that the data/render example, I noticed
the color attribute is actually created in the
code ... IMO this is exactly what we are trying to avoid
with templating. There is no indication in the
template that a new attribute is going to be added
to the tag ...

Not sure what example you mean (I'm not up to date on nevow's current
docs). If the template gives control over to a rendering method, it's
of course fine for the rendering method to perform whatever rendering
alterations it's documented to perform. I disagree that removing
rendering functionality from code is "exactly what we're trying to
avoid": _some_ rendering requires logic (conditionals, loops) and
getting such logic OUT of the HTML is what _I_ see templating as being
mostly about.

There are many finer-grained ways, such as nevow:slot and
nevow:attribute, for the template to suggest exactly what aspects of the
rendering should be controlled by the code.

compare this to the same thing in ZPT:

<span tal:content="name" tal:attributes="style color">Tag Name</span>

With the appropriate renderer (and NS declaration), the nevow equivalent
might be:

<span n:render="name">
<n:attr name="style"> <n:slot name="color"/> </n:attr>
</span>

It could well be simpler, in many ways, depending on how the renderer
method render_name in the code behind this page is specified (where else
but as a style attribute should the color go, for example? so wanting
to spell it out this way may be redundant). But the point is, the logic
is in the code, Python code well out of this HTML file; the role of the
template is to provide the HTML basis, and tag where the current data
comes from, which method renders it, and what spots need to be
identified (as slots, attrs, or patterns).


Alex
 
I

Ian Bicking

Alex said:
What setback? If you've omitted to provide (e.g.) a rendering method
you realize you need, you add it to the Python code and access it with
the appropriate nevow:data="whatever" attribute in the template. No
problem.

Well, this quickly becomes a matter of aesthetics, intractible to argue
about... but anyway...

The question then is, what are you trying to achieve with nevow's
templating? (Or maybe, what do you think is achieved?) I don't think
anyone is achieving the separation of rendering logic from control or
business logic with templating -- rendering logic is too sophisticated
(in real applications) to always rely on a template. In ZPT, some logic
remains in the template; the amount of logic is flexible, and frequently
changes back and forth as the application is developed.

Nevow has a very strong limit to how much logic can go in the page --
and that limit is very low. It's not a limit of zero, since it is more
sophisticated than something like XMLC or the DOM. But it's not
terribly high.

Something that ZPT tries to attempt -- and not entirely successfully --
is a separation of concerns, so that some people can work primarily in
ZPT. I frequently work with people who work entirely in ZPT, sometimes
stretching it further than it should go; but in those cases they are
able to move forward with ZPT, and later we can cooperatively refactor
the code. If I'm the only one comfortable going into the Python (which
is usually the case), I'm not a bottleneck. I'm not sure this is
possible in Nevow.

Now, there might be a valid compromise where a more declarative template
language is acceptable in my sort of situation. Indeed, there is a
problem where control and rendering logic become mixed, which is part of
why other people aren't comfortable going into the Python code (though
there's other reasons, like they aren't familiar with the domain objects
or other pieces of the system). In some ways, I'd like to see a
templating system where I can call the template from my control code
with a minimal, fixed interface, and that there is a small bit of Python
code that does the heavier lifting of making that data acceptable for
the template. To a degree that's already true. OTOH, I don't know if
it's helpful to code alternating row colors for a table in Python, when
it will be necessarily far away from the table that needs the coloring.

Anyway, I'm very suspicious of any system which limits the programmer
for their own good.
 
C

Cliff Wells

Dnia Fri, 22 Oct 2004 03:56:44 -0700, Cliff Wells napisa³(a):


I am using Smarty and I know them very well, but compiled Cheetah Templates
(http://www.cheetahtemplates.org) with its inheritance approach
http://www.cheetahtemplate.org/

is much powerfull and easier to use than Smarty. Cheetah can cache any
indyvidual placeholder or portions of template. Smarty caches only the
entire template output as a unit. Cheetah can be extended using inheritance
approach. Smarty can use only the containment aproach. Cheetah uses Python
with its all benefits. Smarty uses PHP with its messing function names, no
import idea, no namespaces idea, worse unicode library, no docstring, no
good free IDE/RAD editors etc.

First of all, more powerful wasn't the main criteria and easier to use
is arguable depending on your point of view. Smarty strikes a fine
balance between power and simplicity. This is where I feel most of the
Python templating engines fall down. They try to do too much. Smarty
appears to have found the sweet spot between simplicity and power, with
just the right type and amount of built-in utility to do its job. On
top of that, it provides a way to extend itself with custom functions,
so the full power of PHP (broken as that may be) is within reach if you
need it.
My personal feeling on templates is that if you find yourself needing
very powerful features, you're doing it wrong. That's what the parent
programming language is for. My favorite use for templates is storing
them in a database so that the end user can modify a website (via a web
interface) without knowing PHP/Python. Were I shove too much
programming into the template, this wouldn't be possible.

Regards,
Cliff
 
A

Alex Martelli

Ian Bicking said:
The question then is, what are you trying to achieve with nevow's
templating? (Or maybe, what do you think is achieved?) I don't think

In this corner, one or more excellent programmers with the typical
aesthetic and artistic sense of the average programmer: lousy-minus.

In the other corner, one or more excellent graphics designers who
specialize in producing wonderful web pages mixing HTML, style sheets,
graphics, maybe a drop of javascripts for effects and usability -- with
the typical program-design ability of the average artist: lousy-minus.

How do we get great web pages out of this team?

I don't want the HTML, CSS etc to come out of the programmers' work: it
would look bad and be unusable.

I don't want any program design to come out of the artists' work: it
would be crufty and unmaintainable.

What I want is: let the programmers program, and the artists write HTML,
CSS, and the like.

Moreover, in the typical business scenario, the visuals and exact
effects of the pages will be endlessly tweaked and tinkered with,
because that's what clients, marketeers and focus on. The program must
be able to remain stable and just reflect to the browser the current
state of the tweaking and tinkering (ideally without needing to stop and
restart the program, to help the poor artists' lives).

Nevow helps a _lot_ with this scenario. The programmers (with all due
consultation and customer involvement, to be sure) define all the logic
and implement it in Python; _NOT_ in .HTML files, but quite away from
them. What the HTML files need to contain is the mention of the data
sources and renderers that are to be used, out of the 'palette' supplied
by the Python code (nevow:data and nevow:render attributes, data_this
and render_that methods in Python) and markers to identify 'this is the
spot' for the renderer's benefit.

The programmers can develop all logic (with lousy looks) with their own
ugly and nastly HTML files. The artists "just" need to write beautiful,
corporate-standards-du-jour compliant HTML, CSS, etc, with the
appropriate choice of data sources and renderers and 'this is the spot'
markup. Each time a template file is modified, nevow will know to
reload and reparse it next time it's used, which is handy for tweaking
and tinkering purposes.

Nevow has a lot of stuff I haven't needed yet, such as most of stan (can
generate HTML w/o any templating), live-page functionality (use Python
instead of Javascript... but with a round trip every time... well, maybe
on a LAN...), freeform/formless -- maybe I won't ever need it, and it
does not necessarily fit within the above scenario all that well. But
the templating per se seems to me to be just what I need in the above.

anyone is achieving the separation of rendering logic from control or
business logic with templating -- rendering logic is too sophisticated
(in real applications) to always rely on a template.

Right, business vs presentation logic separation is done 'behind' the
template, in Python.
In ZPT, some logic
remains in the template; the amount of logic is flexible, and frequently
changes back and forth as the application is developed.

Nevow has a very strong limit to how much logic can go in the page --
and that limit is very low. It's not a limit of zero, since it is more
sophisticated than something like XMLC or the DOM. But it's not
terribly high.

Bingo: the fewer the better.

Something that ZPT tries to attempt -- and not entirely successfully --
is a separation of concerns, so that some people can work primarily in
ZPT. I frequently work with people who work entirely in ZPT, sometimes

And nevow lets some people (graphics artists specializing in webpages)
work primarily in HTML/CSS/maybe javascript (sigh) for effects &c.
That's exactly how I like it...
stretching it further than it should go; but in those cases they are
able to move forward with ZPT, and later we can cooperatively refactor
the code. If I'm the only one comfortable going into the Python (which
is usually the case), I'm not a bottleneck. I'm not sure this is
possible in Nevow.

I believe it's not -- by design. If program logic must be modified,
programmers (able to work in Python, not a terribly high bar btw) will
be the ones modifying it. I like the idea.

Now, there might be a valid compromise where a more declarative template
language is acceptable in my sort of situation. Indeed, there is a
problem where control and rendering logic become mixed, which is part of

Absolute agreement here: such mixing is a problem. Nevow avoids it.
why other people aren't comfortable going into the Python code (though
there's other reasons, like they aren't familiar with the domain objects
or other pieces of the system). In some ways, I'd like to see a
templating system where I can call the template from my control code
with a minimal, fixed interface, and that there is a small bit of Python
code that does the heavier lifting of making that data acceptable for

As long as said small bit of Python code is in a Python module, I'm fine
with that. Normal inheritance easily lets you split that way the class
that works behind a nevow template: any data supplier or renderer which
is rich or complicated may be stashed away in a base class somewhere
else, and the actual class driving the page need only "little bits" of
overriding/wrapping or adding new simple methods. If you only work with
graphic artists/page designers who are a little bit comfortable with
Python, they can work on retouching the easier, front-side parts, while
all the heavy and please-don't-touch parts are elsewhere and clearly
marked as such. Other such refactorings may quite well be possible; the
"pure" programmers-on-one-side, artists-on-the-other arrangement is
surely not the only one that Nevow enables... just the one that I think
I need, and that I've seen no other templating system enable so well.
the template. To a degree that's already true. OTOH, I don't know if
it's helpful to code alternating row colors for a table in Python, when
it will be necessarily far away from the table that needs the coloring.

No, in my worldview that's CSS's job. Python code only need to emit the
table rows with alternate (CSS) class attributes. The existing sequence
renderer supplied by nevow's infrastructure enables that, btw: write
more than one nevow:pattern="item" tags and render_sequence will render
them in round-robin style.

Anyway, I'm very suspicious of any system which limits the programmer
for their own good.

Ah, you see the guy coding the HTML/CSS/etc as "the programmer". I
don't. The world has even too many webpages made by programmers: it
shows. Let's have some more made by people who know how to do pleasing
and usable graphic designs: they don't need to be programmers (at least
in the "pure" arrangement), they do need to be good artists. Meanwhile
the programmers may have all the aesthetic sense they're famous for,
without thereby spoiling the output pages...


Alex
 
A

Alex Martelli

Cliff Wells said:
My personal feeling on templates is that if you find yourself needing
very powerful features, you're doing it wrong. That's what the parent
programming language is for. My favorite use for templates is storing
them in a database so that the end user can modify a website (via a web
interface) without knowing PHP/Python. Were I shove too much
programming into the template, this wouldn't be possible.

With Nevow you're meant to shove no programming at all into the
template, and enabling the modification of the templates by
non-programmers is a key design goal. (Haven't yet seen any work on
storing them in a DB rather than on the filesystem, or enabling specific
via-web editing thereof rather than the use of normal HTML editing tools
and applications, but that kind of issue seems secondary to the way the
templating system itself is designed).

Nevow is probably not yet mature enough for much more widespread use
than it's getting. But, it _is_ maturing -- and it seems (regarding its
templating subsystem, forgetting for the moment about other stuff such
as live-page klud^H^H^H^H brilliancies;-) consonant with your goals.


Alex
 
J

JZ

Dnia Fri, 22 Oct 2004 14:22:53 -0700, Cliff Wells napisa³(a):
First of all, more powerful wasn't the main criteria and easier to use
is arguable depending on your point of view.

Hmm, so what's the main criteria? :)
Smarty strikes a fine balance between power and simplicity.

It is your personal point of view. Many others consider Smarty for hard to
learn and much too complicated for template system which should be simple
and easy to learn. PHP is itself a template language, so someone could say
there is no need for another template language on top of it. :) Look at
Savant Template System http://phpsavant.com/yawiki/. There are interesting
user notes. Many people seems to be frustrated with Smarty syntax.

But my personal point of view is, that Smarty is the best PHP template
system and it is worth to learn it even if it is not as simple as someone
could expect. I have no problem with Smarty's syntax because I use it for
several years. And if I would have to use PHP I would choose Smarty.

But, the problem is PHP itself has so many bad features that I prefer
rather Python to PHP. And I like Cheetah's inheritence aproach because it
is scales very well for complicated web pages. I can develop internet
application much faster using Python (e.g. using application, very cool and
pythonic Cherry2) and Cheetah. And because extending templates using OO
techniques is tthe feature I like, I would rather choose Cheetach than ZPT,
SimleTAL, Nevow and similar solutions.
This is where I feel most of the Python templating engines fall down.
They try to do too much.

It is not fair to compare one Smarty against several anonymous template
systems for Python. I compare Smarty with Cheetah. Not Cheetah against
PHPLib, and all other better or worse templates for PHP.
Smarty [...] provides a way to extend itself with custom functions,

Savant Templates provides custom plugins using only pure PHP without
another, strange language. :) Savant has markup compiler, so you can invent
any markup system you like.
My personal feeling on templates is that if you find yourself needing
very powerful features, you're doing it wrong.

No. I need power and simplicity working together. And my choice is Cheetah.
It has power and it is enough simple to be easy learned.

Regards,
Jarek http://zabiello.com
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,687
Latest member
IngridXxj

Latest Threads

Top