As someone who learned C first, when I came to Python everytime I read
about a new feature it was like, "Whoa! I can do that?!" Slicing, dir(),
getattr/setattr, the % operator, all of this was very different from C.
I'm curious -- what is everyone's favorite trick from a non-python
language? And -- why isn't it in Python?
Here's my current candidate:
So the other day I was looking at the language Lua. In Lua, you make a
line a comment with two dashes:
-- hey, this is a comment.
And you can do block comments with --[[ and ---]].
--[[
hey
this
is
a
big
comment
--]]
This syntax lets you do a nifty trick, where you can add or subtract a
third dash to change whether or not code runs:
--This code won't run because it's in a comment block
--[[
print(10)
--]]
--This code will, because the first two dashes make the rest a comment,
breaking the block
---[[
print(10)
--]]
So you can change whether or not code is commented out just by adding a
dash. This is much nicer than in C or Python having to get rid of """ or
/* and */. Of course, the IDE can compensate. But it's still neat :)
Jul 19 '05
134 6046
On Fri, 24 Jun 2005 00:55:38 -0600, Joseph Garvin wrote: I'm curious -- what is everyone's favorite trick from a non-python language? And -- why isn't it in Python?
Long ago, I used to dabble in Forth. You could say, the entire Forth
language was a trick :-) It was interesting to be able to define your own
compiler commands, loop constructs and so forth.
One of the things I liked in Pascal was the "with" keyword. You could
write something like this:
with colour do begin
red := 0; blue := 255; green := 0;
end;
instead of:
colour.red := 0; colour.blue := 255; colour.green := 0;
Okay, so maybe it is more of a feature than a trick, but I miss it and it
would be nice to have in Python.
--
Steven
I supose it will be very, very hard to find a trick
which turns out useful in Python. The current
set of features is from my point of view very
well designed out of the experience with many
other languages, so maybe you can use Python
as reference telling you which tricks are really
useful in programming and which are just bad,
non-Pythonic style.
At least I am myself out of ideas, else I had
proposed a PEP already, to get it or have
provided a module for it.
So feel free to poke around with Python.
if(not \
"--[[" == "--[["):
# block of code
# --]]
as (already suggested) will also do the
trick with the block comment.
By the way, I personally find
--[[
good style comment block
]]--
or
[[--
good style comment block
--]]
much more intuitive than
--[[
bad style comment block
--]]
Claudio
"Joseph Garvin" <k0*****@kzoo.e du> schrieb im Newsbeitrag
news:ma******** *************** *************** @python.org... Claudio Grondi wrote:
And you can do block comments with --[[ and ---]].
I am very happy not to have such "tricks" in Python.
Any other (useful) suggestions?
Claudio
I'm glad and all that not everyone shares my enthusiasm over Lua's trick, and I'm glad that C/C++ can do it, but the original issue was non-python language tricks in general. Lets keep the thread on track.
So far we've got lisp macros and a thousand response's to the lua trick. Anyone else have any actual non-python language tricks they like?
Yeesh.
"Joseph Garvin" <k0*****@kzoo.e du> schrieb im Newsbeitrag news:ma******* *************** *************** *@python.org...
As someone who learned C first, when I came to Python everytime I read about a new feature it was like, "Whoa! I can do that?!" Slicing, dir(), getattr/setattr, the % operator, all of this was very different from C.
I'm curious -- what is everyone's favorite trick from a non-python language? And -- why isn't it in Python?
Here's my current candidate:
So the other day I was looking at the language Lua. In Lua, you make a line a comment with two dashes:
-- hey, this is a comment.
And you can do block comments with --[[ and ---]].
--[[ hey this is a big comment --]]
This syntax lets you do a nifty trick, where you can add or subtract a third dash to change whether or not code runs:
--This code won't run because it's in a comment block --[[ print(10) --]]
--This code will, because the first two dashes make the rest a comment, breaking the block ---[[ print(10) --]]
So you can change whether or not code is commented out just by adding a dash. This is much nicer than in C or Python having to get rid of """ or /* and */. Of course, the IDE can compensate. But it's still neat :)
>with colour do begin red := 0; blue := 255; green := 0; end;
instead of:
colour.red := 0; colour.blue := 255; colour.green := 0;
Why not:
from color import *
red := 0
blue := 255
green := 0
....
On Fri, 24 Jun 2005 06:13:00 -0700, utabintarbo wrote: with colour do begin red := 0; blue := 255; green := 0; end;
instead of:
colour.red := 0; colour.blue := 255; colour.green := 0;
Why not:
from color import *
red := 0 blue := 255 green := 0
Because colour is not a module, it is a record. Sorry, I assumed that
people would be familiar with Pascal records (similar to C structs) and
it wouldn't need explanation. My bad :-(
The closest example in Python would be:
def class Colour:
def __init__(self, blue=0, green=0, red=0):
self.blue = blue
self.green = green
self.red = red
which would become something like:
def class Colour:
def __init__(self, blue=0, green=0, red=0):
# pseudo-Python code borrowing concept "with" from Pascal
with self:
blue = blue
green = green
red = red
And now you can see why Python doesn't support this idiom.
--
Steven.
Joseph Garvin wrote: I'm curious -- what is everyone's favorite trick from a non-python language? And -- why isn't it in Python?
You can try out new features yourself now using various python
extensions or descendants: http://livelogix.net/logix/
- macros, no statement/expression distinction http://students.ceid.upatras.gr/~sxanth/pyc/
- assignments are expressions, you can try other features never added
python like a ternary operator (x ? true action:false action) http://boo.codehaus.org/
assignments are expressions, macros, static typing, no more "self"
required, ... ut*********@gma il.com wrote: with colour do begin red := 0; blue := 255; green := 0; end;
instead of:
colour.red := 0; colour.blue := 255; colour.green := 0;
Why not: from color import *
red := 0 blue := 255 green := 0
What do you think this actually does?
It doesn't do anything remotely resembling either of the things quoted
above. In fact, the "from color import *" line is pretty much useless here.
-Peter
> def class Colour: def __init__(self, blue=0, green=0, red=0): # pseudo-Python code borrowing concept "with" from Pascal with self: blue = blue green = green red = red
And now you can see why Python doesn't support this idiom.
Maybe it would make more sense if it was done a la Visual Basic
with self:
.blue = blue
.green = green
.red = red
requiring a dot to be typed removes the ambiguity and gives the IDEs a
chance to Intellisense-ify your coding.
infidel wrote: def class Colour: def __init__(self, blue=0, green=0, red=0): # pseudo-Python code borrowing concept "with" from Pascal with self: blue = blue green = green red = red
And now you can see why Python doesn't support this idiom.
Maybe it would make more sense if it was done a la Visual Basic
with self: .blue = blue .green = green .red = red
requiring a dot to be typed removes the ambiguity and gives the IDEs a chance to Intellisense-ify your coding.
Some made a python recipe emulating this I believe. The python cookbook
search engine is down though so I cannot find the link.
At one point Guido van Rossum was advocating this use of "with" as well,
I believe: http://mail.python.org/pipermail/pyt...ch/043545.html
But I don't think it is being considered now. I think now "with" is
being proposed for something more like VB and C#'s "using" statement.
It automatically disposes of a resource when done with it: http://wiki.python.org/moin/WithStatement
Steven D'Aprano <st***@REMOVETH IScyber.com.au> writes: On Fri, 24 Jun 2005 00:55:38 -0600, Joseph Garvin wrote:
I'm curious -- what is everyone's favorite trick from a non-python language? And -- why isn't it in Python?
Long ago, I used to dabble in Forth. You could say, the entire Forth language was a trick :-) It was interesting to be able to define your own compiler commands, loop constructs and so forth.
One of the things I liked in Pascal was the "with" keyword. You could write something like this:
with colour do begin red := 0; blue := 255; green := 0; end;
instead of:
colour.red := 0; colour.blue := 255; colour.green := 0;
Okay, so maybe it is more of a feature than a trick, but I miss it and it would be nice to have in Python.
.... that quickly becomes quite messy:
with A do begin
.....
with B do begin
.....
with C do begin
x := y;
end;
end;
end;
.... and now you need to check the declarations of C, B, and A to
actually see what is assigned to what.
Worse yet, adding field 'x' to 'C' will (silently) break the code :(
I don't think it would be that nice to have it in Python.
--
Sergei.
On Fri, 24 Jun 2005, Joseph Garvin wrote: Claudio Grondi wrote:
So far we've got lisp macros and a thousand response's to the lua trick. Anyone else have any actual non-python language tricks they like?
Higher-order functions like map, filter and reduce. As of Python 3000,
they're non-python tricks. Sigh - i guess it's time for me to get to know
list comprehensions a bit better.
The one thing i really do miss is method overloading by parameter type. I
used this all the time in java, and it really notice the lack of it
sometimes in python. No, it's not really possible in a typeless language,
and yes, there are implementations based on decorators, but frankly,
they're awful.
Yeah, and i'm with "if False:" for commenting out chunks of code.
tom
--
.... but when you spin it it looks like a dancing foetus! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Randy Given |
last post by:
What is your favorite Java development environment? What others have you
tried? What are some pros and cons of each? Thanks!
|
by: Vic Cekvenich |
last post by:
Here is my favorite tag: http://displaytag.sf.net and see examples (in upper
right). Grid, Sorting, nested, group, export, everything you like, plus it's
free.
Here is example code of how I used it:
http://cvs.sourceforge.net/viewcvs.py/basicportal/bPproj/bP/WEB-INF/pgs/forums/ArticleLst.jsp
You can view here run time by clicking here:...
|
by: Matthew Louden |
last post by:
I want to make a link that allows users to add my web site to the favorite
in IE. Anyone knows how to do that?
|
by: Scott McNair |
last post by:
What's your favorite bit of "short-cut" code that you use?
One of my favorite shortcuts I'll use when coding a page is to create a sub
along the lines of the following:
Sub Print(myText)
Response.Write myText & vbcrlf
End Sub
And then I use Print instead of Response.Write thru my code.
|
by: edunetgen |
last post by:
I have a Web Aplication.
I want an own image when an user adds me to favorite.
Thanks,
Edu
| |
by: zhaoyandong |
last post by:
One of my interviewers ask me
"Two favorite features of C++, and over-rated, and misued features"
Could anybody give me some advice on this?
Thanks
|
by: Les |
last post by:
In ancient times I had a desire to make a game and I
started to do so, I quickly found the project was beyond
the hardware's capability for that era (even in
assembler) and had to shelf the project. I retried in the
dark ages of computers, when I was in college, and got
much further before realizing the same thing was taking
place and...
|
by: Jensen bredal |
last post by:
Hello,
I need to offer the visitors of my siste to add it to the favorite list but
only when it is ot there.
the code below:
window.external.AddFavorite(location.href, document.title)
add the link.
How can i see if the current link is already there?
|
by: clintonG |
last post by:
Want to give up your most treasured posession?
Post the URL for your favorite RegEx library you've found most useful for
validating form input.
<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/
|
by: fiversen |
last post by:
Hello,
I have a site for the collegue with a football winning game.
...fussball.html
there I redirect the user to an cgi-page
../f11.cgi
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |