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 6137
"Konstantin Veretennicov" <kv***********@ gmail.com> wrote: On 6/25/05, Steven D'Aprano <st***@removeth iscyber.com.au> wrote: On Sat, 25 Jun 2005 17:41:58 +0200, Konstantin Veretennicov wrote:
On 6/25/05, Mandus <ma****@gmail.c om> wrote: > It is really a consensus on this; that > removing map, filter, reduce is a good thing? It will render a whole lot > of my software unusable :(
I think you'll be able to use "from __past__ import map, filter, reduce" or something like that :) They don't have to be built-in.
More likely they will be moved to something like itertools than "__past__".
Or just define them yourself:
def map(f, seq): return [f(x) for x in seq]
def filter(p, seq): return [x for x in seq if p(x)]
def reduce(f, seq, zero): r = zero for x in seq: r = f(r, x) return r
FWIW, these don't exactly reproduce behaviour of current built-ins. Filter, for instance, doesn't always return lists and map accepts more than one seq... Just my $.02.
- kv
If they go to itertools, they can simply be:
def map(f, *iterables):
return list(imap(f,*it erables))
def filter(f, seq):
return list(ifilter(f, seq))
George
Sun, 26 Jun 2005 04:36:51 +1000 skrev Steven D'Aprano: On Sat, 25 Jun 2005 17:41:58 +0200, Konstantin Veretennicov wrote:
On 6/25/05, Mandus <ma****@gmail.c om> wrote: It is really a consensus on this; that removing map, filter, reduce is a good thing? It will render a whole lot of my software unusable :(
I think you'll be able to use "from __past__ import map, filter, reduce" or something like that :) They don't have to be built-in.
More likely they will be moved to something like itertools than "__past__".
Or just define them yourself:
def map(f, seq): return [f(x) for x in seq]
def filter(p, seq): return [x for x in seq if p(x)]
def reduce(f, seq, zero): r = zero for x in seq: r = f(r, x) return r
sure - that will be possible. But the main point (for me) is to avoid
the horrible slow for-loop in python (reduce...). By using the builtin reduce, I
move the for-loop into the c-code which performs better.
map and filter with list-comprehensions is probably ok - I use
list-comprehensions a lot, but somehow like the syntax of map/filter
better.
When it comes to lambdas, I am not so sure. I use them all the time, and
I will certainly miss them, and I have used lambdas in ways which at
least take som tinkering to translate to normal def's (or rather
closures). But I am not sure yet whether I have cases which are
impossible to translate (hey, nothing is impossible, some things just
take a bit more time).
Oh, and by the way, I use python to solve PDEs :)
But as someone else said, this will take some time. And I can always put
the c-function back in my self when that time comes.
Another important point, which it seems Guido does not fancy very much,
is that Python can be an ok functional style language for those who like
it. I very much enjoy the concept of using different programming styles
within the same language. It is mostly a convenience - I admit that -
but it makes me more productive. I'll be very sorry if we take that away
from python.
Maybe I am to late to change Guido on this - but if we are many, maybe
we can!
--
Mandus - the only mandus around.
Sat, 25 Jun 2005 16:06:57 GMT skrev Lee Harr: 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.
Couldnt there just be a "functional " module ?...
from functional import map, filter, reduce
but lambda is grammar, so probably not so easy to import?
--
Mandus - the only mandus around.
Why overload when you can use class methods?
Steven D'Aprano wrote: 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.
With PEP343 (I guess in Python 2.5), you will be able to do something like:
with renamed(colour) as c:
c.red = 0; c.blue = 255; c.green = 0
I think however it is bad. Better solutions to me would be:
colour.setRgb(0 , 255, 0)
or
c = myVeryLongNameC olour
c.red = 0; c.blue = 255; c.green = 0
Regards,
Nicolas
Sat, 25 Jun 2005 21:30:26 +0200 skrev Peter Otten: Mandus wrote:
By using the builtin reduce, I move the for-loop into the c-code which performs better.
No. There is no hope of ever writing fast code when you do not actually measure its performance.
I do.
--
Mandus - the only mandus around.
But by using the builtin reduce, you need to specify a function, which
probably slows it down more than any speed-up from the loop in C.
25 Jun 2005 13:15:16 -0700 skrev Devan L: But by using the builtin reduce, you need to specify a function, which probably slows it down more than any speed-up from the loop in C.
Sounds reasonable, but not always the case, especially when dealing with
numpy arrays. At least that what some of my test shows. But then I
should probably write c-functions that deals with the numeric arrays
anyway.
Besides, functions like 'operator.add' is also in c, maybe that helps.
But I admit it's not a perfect example.
--
Mandus - the only mandus around.
Devan L wrote: But by using the builtin reduce, you need to specify a function, which probably slows it down more than any speed-up from the loop in C.
Not if the function is from an extension module. For some applications,
this can be quite common.
Of course, in a Python 3000 world, nothing stops anyone from using their
own extension module implementing map, filter, and reduce if they really
want to. TSBOOOWTDI in the language/stdlib, but it shouldn't stop anyone
from using other ways to do it that aren't in the stdlib if the
tradeoffs are right for them.
--
Robert Kern rk***@ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Mandus wrote: 25 Jun 2005 13:15:16 -0700 skrev Devan L: But by using the builtin reduce, you need to specify a function, which probably slows it down more than any speed-up from the loop in C. Sounds reasonable, but not always the case, especially when dealing with numpy arrays. At least that what some of my test shows. But then I should probably write c-functions that deals with the numeric arrays anyway.
Besides, functions like 'operator.add' is also in c, maybe that helps.
Yes, the C-coded operator.mul() was the counterexample that John Lenton came
up with when I challenged the speed advantage of reduce() over the
equivalent for-loop.
But I admit it's not a perfect example.
Python is more about readability than raw speed, and I prefer a for-loop
over reduce() in that respect, too. If you need the best possible
efficiency you would probably have to code the loop in C. Incidentally, for
add() this has already been done with the sum() builtin.
Peter 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:
http://basebeans.com/do/channelsPg
|
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 shelved the project again. In hopes that this
new PC technology may yeild something interesting.
...
|
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: 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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
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.
| |