473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Python's annoyance.

Expand|Select|Wrap|Line Numbers
  1. PEP 292 adds a Template class to the string module that uses "$" to
  2. indicate a substitution. Template is a subclass of the built-in
  3. Unicode type, so the result is always a Unicode string:
  4.  import string
  5.  t = string.Template('$page: $title')
  6.  t.substitute({'page':2, 'title': 'The Best of Times'})
  7. u'2: The Best of Times'
  8.  
  9. If a key is missing from the dictionary, the substitute method will
  10. raise a KeyError. There's also a safe_substitute method that ignores
  11. missing keys:
  12.  t = string.SafeTemplate('$page: $title')
  13.  t.safe_substitute({'page':3})
  14. u'3: $title'
  15.  

Expand|Select|Wrap|Line Numbers
  1. As a slightly more realistic example, the following decorator
  2. checks that the supplied argument is an integer:
  3.  
  4. def require_int (func):
  5. def wrapper (arg):
  6. assert isinstance(arg, int)
  7. return func(arg)
  8.  
  9. return wrapper
  10.  
  11. @require_int
  12. def p1 (arg):
  13. print arg
  14.  
  15. @require_int
  16. def p2(arg):
  17. print arg*2
  18.  


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.
Jul 18 '05 #1
8 1302
caroundw5h wrote:
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:
------------------------------------------------------------
string.Template('$page: $title').substitute(dict(page=2, title='The Best of Times'))
'2: The Best of Times' '%(page)s: %(title)s' % dict(page=2, title='The Best of Times') '2: The Best of Times'

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

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
Jul 18 '05 #2
caroundw5h wrote:
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.
Jul 18 '05 #3
On Wed, 24 Nov 2004 10:47:11 -0800, caroundw5h wrote:
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[s]+p(n-1,np(s,np))or[]

(http://groups.google.com/groups?hl=e...99%40tim#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-...rtem.cgi?id=11)
(http://perlgolf.sourceforge.net/TPR/0/6/)

Jul 18 '05 #4
Hi !

Just for fun (?) :

A qsort in "J-programming-language" :

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

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


Jul 18 '05 #5
caroundw5h wrote:
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.
--
Giovanni Bajo
Jul 18 '05 #6
"Giovanni Bajo" <no***@sorry.com> wrote in message news:<iB*********************@twister2.libero.it>. ..
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
Jul 18 '05 #7
On Wed, 24 Nov 2004 23:02:01 +0100, Michel Claveau - abstraction
méta-galactique non triviale en fuite perpétuelle. wrote:
Hi !

Just for fun (?) :

A qsort in "J-programming-language" :

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

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 :-)

--
Christopher

Jul 18 '05 #8
Michele Simionato <mi***************@gmail.com> wrote:

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 ...>

Right.

Giovanni Bajo
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Fitzsimons | last post by:
Hi, I want to search an ordered text file and list web links such as : Input like ; A Folder Freeware ftp://ftp.eunet.bg/pub/simtelnet
12
by: Don Bruder | last post by:
A week or two ago, I asked here about porting Python to C. Got some good answers (Note 1) but now I've got another question. Actually, more a request for clarification of a topic that both the...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
5
by: Paradox | last post by:
In general I love Python for text manipulation but at our company we have the need to manipulate large text values stored in either a SQL Server database or text files. This data is stored in a...
17
by: hanumizzle | last post by:
I have used Perl for a long time, but I am something of an experimental person and mean to try something new. Most of my 'work' with Vector Linux entails the use of Perl (a bit of a misnomer as it...
42
by: redefined.horizons | last post by:
I'm coming from a Java background, so please don't stone me... I see that Python is missing "interfaces". The concept of an interface is a key to good programming design in Java, but I've read...
267
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at...
38
by: flifus | last post by:
Hi all. I'm learning python these days. I'm going to use this thread to post, from time to time, my annoyances with python. I hope someone will clarify things to me where I have misunderstood them....
11
by: wongjoekmeu | last post by:
Are there any completely free developent tools for python scripts like IDLE. I have used IDLE , but I want to try out others also. I saw stuff like PyCrust, but I don't see that it can run the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.