Python's annoyance.

C

caroundw5h

Code:
PEP 292 adds a Template class to the string module that uses "$" to
indicate a substitution. Template is a subclass of the built-in
Unicode type, so the result is always a Unicode string:
u'2: The Best of Times'

If a key is missing from the dictionary, the substitute method will
raise a KeyError. There's also a safe_substitute method that ignores
missing keys:
u'3: $title'


Code:
As a slightly more realistic example, the following decorator
checks that the supplied argument is an integer:

def require_int (func):
    def wrapper (arg):
        assert isinstance(arg, int)
        return func(arg)

    return wrapper

@require_int
def p1 (arg):
    print arg

@require_int
def p2(arg):
    print arg*2



Serioulsy, one of python's main selling points is its elegant syntax,
non perl like, non C like. If it can't live up to it. I guess i might
as well use perl or ruby or server side javascript.

how annoying.
 
S

Steven Bethard

caroundw5h said:
Serioulsy, one of python's main selling points is its elegant syntax,
non perl like, non C like. If it can't live up to it. I guess i might
as well use perl or ruby or server side javascript.

While I'm not a fan of the decorator syntax or the $syntax in
string.Template, you're not forced to use either of these:

Standard Python formatting vs string.Template formatting:
------------------------------------------------------------Best of Times'))
'2: The Best of Times''2: The Best of Times'

Post-function syntax vs. decorator syntax:
------------------------------------------------------------.... def wrapper(arg):
.... assert isinstance(arg, int)
.... return func(arg)
.... return wrapper
........ def p1(arg):
.... print arg
........ print arg
.... Traceback (most recent call last):
File "<interactive input>", line 1, in ?
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 3, in wrapper
AssertionError


Personally, I never saw the gain of the string.Template class -- in most
of my uses I can write code that's just as clear and concise using
%-style formatting. I do however make use of the decorator syntax --
it's nice to have things like classmethod indicated at the top of the
function instead of at the bottom.

Steve
 
D

Doug Holton

caroundw5h said:
Serioulsy, one of python's main selling points is its elegant syntax,
non perl like, non C like. If it can't live up to it. I guess i might
as well use perl or ruby or server side javascript.

I think it's still better than perl or ruby, but anyway,
here are your two examples in boo: http://boo.codehaus.org/

1.
t = "${page}: ${title}"

2.
def p2(arg as int):
print arg*2

Probably you'll be able to do similar things in Python 3.0, too, but
that is years away.
 
G

Giovanni Bajo

caroundw5h said:
Serioulsy, one of python's main selling points is its elegant syntax,
non perl like, non C like. If it can't live up to it. I guess i might
as well use perl or ruby or server side javascript.
how annoying.

In fact, I find the decorator syntax awful. But I know this was already
discussed to a large extent, so there must be a very good reason to use @.
Which I totally miss, but hey.
 
J

Jeremy Bowers

Serioulsy, one of python's main selling points is its elegant syntax, non
perl like, non C like. If it can't live up to it. I guess i might as well
use perl or ruby or server side javascript.

There's a rather large difference between

@require_int
@dynamic_dispatch('add', int)
@author("Steven Q. Dude")
@whack_a_hacka_ding_dong(with_fruit=True, sledgehammer=5)
@register(zodb & mysql_storage)
def increment(i):
return i + 1

and

@require_int
def advanceCounter(index):
objToAdvance = objStorageArray[index]
obj.prep(prepType = ADVANCE)
try:
obj.advance(1)
except IOError:
obj.lazyAdvance(1)

return obj

One guess which is the more common case.

I can make any language look bad by focusing on the nasty cases. I mean,
why not complain about

p=lambda n,s=2,np=lambda s,f:reduce(lambda x,y:x and y,map(lambda
x,y:x%y,[s+1]*(s/2),range(2,s/2+2)))and s+1or f(s+1,f
):n>0and+p(n-1,np(s,np))or[]

(http://groups.google.com/groups?hl=...eekm=000001bd7a3a$f205cc40$18472399@tim#link3)

Obviously, any language that allows that isn't clean.

You have to take the gestalt of real code written in the language, not
contrive cases focusing on the worst part of the language. Python's near
the top of the heap. Javascript would fare better if so many inexperienced
programmers weren't bashing away in it, and if so many extant programs in
it weren't bogged down by browser checking code brought on by wildly
divergent DOMs.

If you insist on using such faulty metrics, though, hie thee hence to
Perl, that epitome of elegant syntax and more power to you. I leave you
with this closing benediction in honor of the might syntactical
cleanliness of Perl:

@==sort@$=map$_.shift@=,@@for@@=/\pL|,/g;$_=@$[$_]

(http://perlgolf.sourceforge.net/cgi-bin/PGAS/post_mortem.cgi?id=11)
(http://perlgolf.sourceforge.net/TPR/0/6/)
 
M

Michele Simionato

Giovanni Bajo said:
In fact, I find the decorator syntax awful. But I know this was already
discussed to a large extent, so there must be a very good reason to use @.
Which I totally miss, but hey.

Well, there is very good reason: Guido likes it!

<no further comments ...>

Michele Simionato
 
C

Christopher Koppler

Hi !

Just for fun (?) :

A qsort in "J-programming-language" :

qsort =: ]`(($:mad::((}.<:{.)#}.)),{.,($:mad::((}.>{.)#}.)))@.(*@#)



More infos here : http://en.wikipedia.org/wiki/J_programming_language


Well, as J is mostly APL dressed up in ASCII, it's bound to be readable...
I've found it convenient for some numerical problems though. And getting
my head around it definitely broadened my perspective - so much in fact,
that I mostly run screaming from most APL-like languages since I found
Python :)
 

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
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top