473,769 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Favorite non-python language trick?

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 6125
On 24 Jun 2005 19:09:05 +0400, Sergei Organov <os*@javad.ru > wrote:
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:


- When abused -
with A do begin
.....
with B do begin
.....
with C do begin
x := y;
end;
end;
end;


Like many features that can be helpful when used well, and harmful when used
poorly, it's not a simple question whether it should be in any given language.
It also makes sense to consider whether other features already in the language
can fill the same need (though I don't know Python well enough to address that
yet). Even though I like "With" in VB and use it often, I always consider its
use a warning that perhaps that code should be factored into the class
somehow.

Jul 19 '05 #91
> if a.value == True:
return a if not database.connec t == error:
database.query( q)
Yeah, yeah, I know that :-)
What I mean is that most of the time I find the code more "readable" (I
know that more readable code ain't better code, but it helps when you
work with other people...).
"unless" seems to become "while not", as opposed to "if not". Should be
more consistent.


My mistake :-S
The comment in the code was erroneous, I shouldn't write messages to
the list while asleep ^_^

'unless' works as 'if not', not as 'while not'. Sorry for that :-)

Anyway, it does improve readability. I know that it doesn't necessarily
makes code better, but it's a nice "trick" that I like :-)

Other nice thing about ruby is declaring regexps as /regexp/ rather
than having to re.compile("reg exp"), and having a built-in operator to
match against them (of course, as everything in ruby, overloadable in
each class :-))

-NIcolas

Jul 19 '05 #92
Tom Anderson <tw**@urchin.ea rth.li> writes:
On Fri, 24 Jun 2005, Roy Smith wrote:
Tom Anderson <tw**@urchin.ea rth.li> wrote:
The one thing i really do miss is method overloading by parameter
type. I used this all the time in java

You do things like that in type-bondage languages

I love that expression. I think it started out as 'bondage and
discipline languages', which is even better. I'm going to start
referring to python as a 'sluttily typed' language.


Since the user is the one bound with B&D languages, they are clearly
tops. Which makes Python a bottom.

<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #93
Hi All--

Mike Meyer wrote:

Since the user is the one bound with B&D languages, they are clearly
tops. Which makes Python a bottom.


Well, we certainly hope Python has a safe word.

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
Jul 19 '05 #94
Sun, 26 Jun 2005 08:35:58 +0200 skrev Peter Otten:
Steven D'Aprano wrote:
On Sat, 25 Jun 2005 21:30:26 +0200, Peter Otten wrote:
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.
Good grief! You've been spying on Mandus! How else could you possibly know
that he doesn't measure performance? Are you running a key-logger on his
machine? *wink*


His mentioning reduce() as a performance panacea was a strong indication
even without looking over his shoulders. He filled in some conditions in a
later post, but "[u]sing reduce ... performs better [than a for-loop]" is
just wrong.


Ok - so sometimes reduce() for convenience (nha, that's just me...),
sometimes for performance. In some cases clever use of map/reduce/etc.
have given a good speedup - say 4 times that of for-loops. But going to
C can give 10 to 100 times speed up over that again... So it depends how
important the performance is. Going to C/Fortran is always a bit more
hassel, while reduce is something you can stick in your interactive
session to finish the work rather before than after lunch :)

[snip]
Isn't it reasonable to just say, "I use join because it is faster than
adding strings" without being abused for invalid optimization?
OK, I am making a guess: "".join(strings ) is more often faster than
naive string addition than reduce() wins over a for-loop.


you're probably right.
I don't think my pointed comment qualifies as "abuse", by the way.


neither think I.
--
Mandus - the only mandus around.
Jul 19 '05 #95
Joseph Garvin wrote:
'm curious -- what is everyone's favorite trick from a non-python
language? And -- why isn't it in Python?

I use constraints programming at work, Check out "System Verilog" or
OZ/Mozart.

It would be great if this style of programming could be added to
Python.

It is a declarative programming style
(http://en.wikipedia.org/wiki/Declarative_programming), in which you
can state what discrete values constrained values can take , say in a
list. Give a set of functions that must be true for the variables then
sit back and generate one or many sets of variable values that fit the
constraints.

The OZ homepage has an example for solving the eight queens problem:

http://www.mozart-oz.org/documentati...scripts.queens

My use would be for testing. In the electronic design automation
fields, there are several proprietary languages with this feature, here
is an example written in Cadence's Specman 'e' language:
http://www.asic-world.com/specman/specman_one_day2.html
Cheers, Paddy.

Jul 19 '05 #96

"Paddy" <pa*******@nets cape.net> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.com...
The OZ homepage has an example for solving the eight queens problem:


Buried in the generator test module (something like
lib/test/test_generators .py) are solutions for both 8Queens and Knight's
Tour. You might find these of interest.

Terry J. Reedy

Jul 19 '05 #97
Sadly, its not a solution that I'm after, but a particular toolkit that
can be used for solving that type of problem.

- Pad.

Jul 19 '05 #98
Joseph Garvin <k0*****@kzoo.e du> writes:
I'm curious -- what is everyone's favorite trick from a non-python
language?
Metapost solution of linear equations:
x1+9=x2-8=2;
And -- why isn't it in Python?


I'd like to know too.

--
Brian (remove the sport for mail)
http://www.et.web.mek.dtu.dk/Staff/be/be.html
Jul 19 '05 #99
On Wednesday 29 June 2005 10:51 pm, Paddy wrote:
Joseph Garvin wrote:
'm curious -- what is everyone's favorite trick from a non-python
language? And -- why isn't it in Python?

I use constraints programming at work, Check out "System Verilog" or
OZ/Mozart.

It would be great if this style of programming could be added to
Python.


Check out:
http://www.logilab.org/projects/python-logic/

In short, it has been already.

This is something pretty new to me, so I can't comment on how well
it would meet your expectations, but I see now that the site does mention
OZ/Mozart as comparables.

--
Terry Hancock ( hancock at anansispacework s.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 19 '05 #100

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

Similar topics

3
2825
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!
0
3487
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
2
1454
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?
9
1539
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.
1
1290
by: edunetgen | last post by:
I have a Web Aplication. I want an own image when an user adds me to favorite. Thanks, Edu
2
1514
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
2
1955
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. ...
3
1036
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?
1
1128
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/
4
2170
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
0
9589
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, 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...
0
10211
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, 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...
0
9863
tracyyun
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...
1
7408
isladogs
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...
0
6673
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();...
0
5298
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...
1
3958
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
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.