473,769 Members | 2,337 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
With the exception of reduce(lambda x,y:x*y, sequence), reduce can be
replaced with sum, and Guido wants to add a product function.

Jul 19 '05 #111
On Fri, 24 Jun 2005 21:17:09 GMT, rumours say that Ron Adam
<rr*@ronadam.co m> might have written:
I think some sort of inline or deferred local statement would be useful
also. It would serve as a limited lambda (after it's removed), eval
alternative, and as a inlined function in some situations as well I think.

Something like:

name = defer <expression>

then used as:

result = name()

The expression name() will never have arguments as it's meant to
reference it's variables as locals and probably will be replaced
directly with names's byte code contents at compile time.

Defer could be shortened to def I suppose, but I think defer would be
clearer. Anyway, it's only a wish list item for now.


This is similar:

http://groups-beta.google.com/group/...c884147852d23d
--
TZOTZIOY, I speak England very best.
"Dear Paul,
please stop spamming us."
The Corinthians
Jul 19 '05 #112
Joseph Garvin wrote:
I'm curious -- what is everyone's favorite trick from a non-python
language? And -- why isn't it in Python?


I'm not aware of a language that allows it, but recently I've found
myself wanting the ability to transparently replace objects. For
example, if you have a transparent wrapper class around a certain
object, and then determine that you no longer need to wrap the object,
you can say the magic incantation, and the wrapper instance is replaced
by what it is wrapping everywhere in the program. Or you have a complex
math object, and you realize you can reduce it to a simple integer, you
can substitue the integer for the math object, everywhere.

I mainly look for it in the "object replaces self" form, but I guess you
could also have it for arbitrary objects, e.g. to wrap a logging object
around a function, even if you don't have access to all references of
that function.

Why isn't it in Python? It's completely counter to the conventional
object semantics.
Jul 19 '05 #113
Joseph Garvin wrote:

I'm curious -- what is everyone's favorite trick from a non-python
language? And -- why isn't it in Python?


1. Lisp's "dynamicall y scoped" variables (Perl has them, and calls them
"local", but as far as I've seen their use their is discouraged). These
are global variables which are given time-local bindings. That is,
structuring the syntax after what's used for globals,

x=10
def foo():
# No need to define x as it is only read -- same as globals
print x

def bar():
dynamic x
x = 11
foo()

def baz():
bar() # prints 11
foo() # prints 10; the binding in bar is undone when bar exits

This feature makes using "globals" sensible, providing a way to avoid
many important uses (and some say, misuses) of objects if you are so
inclined. It allows you to do some things better than objects do,
because it does to library parameters, what exceptions do to return
codes: instead of passing them in all the way from outside until a
piece of code which actually uses them, they are only mentioned where
you set them and where you really need to access them.

It would not be too hard to implement a version of this (inefficiently)
in the existing language, if frame objects could carry a modifiable
dictionary.

I suppose it is not in Python because (most) Pythoners are not looking
(hard enough) for alternatives to OOP.

2. Prolog's ability to add operators to the language. Though this
facility is quite clanky in Prolog (because there is no elegant way to
specify precedence), the idea is appealing to me. It would allow a
better implementation of my (awkward, granted) recipe for adding logic
programming constructs to Python. It is not in the language because it
might fragmentize it, and because it is very hard to make
recursive-descent parsers like CPython's programmable this way.

3. Lisp's Macros, of course, which have been mentioned already in this
thread. Even Boo-like macros, which are nowhere as strong as Lisp's,
would be very useful. Not in the language, besides its being hard in
any non-lisp-like language, for the reasons mentioned for adding
operators.

On the other hand, there's no end to the features I wish I could copy
from Python to other languages...

Jul 19 '05 #114
Rocco Moretti wrote:
Joseph Garvin wrote:

I'm not aware of a language that allows it, but recently I've found
myself wanting the ability to transparently replace objects....
I mainly look for it in the "object replaces self" form, but I guess you
could also have it for arbitrary objects, e.g. to wrap a logging object
around a function, even if you don't have access to all references of
that function.

Why isn't it in Python? It's completely counter to the conventional
object semantics.


Actually this is the old (and terrifying) Smalltalk message 'becomes:'.
There is a concrete reason it is not in python: objects are represented
as pointers to their data structures, do not have identical sizes, and
therefore cannot be copied into each others data space. Smalltalk
implementations often have a level of indirection that allows it to
simply tweak an indirection table to implement this method.

The reason I find it terrifying is that I can be passed an object,
place it in a dictionary (for example) based on its value, and then
it can magically be changed into something else which does not fit
in that spot in the dictionary.

--Scott David Daniels
Sc***********@A cm.Org
Jul 19 '05 #115
"Shai" <sh**@platonix. com> writes:
Joseph Garvin wrote:

I'm curious -- what is everyone's favorite trick from a non-python
language? And -- why isn't it in Python?
1. Lisp's "dynamicall y scoped" variables (Perl has them, and calls them
"local", but as far as I've seen their use their is discouraged). These
are global variables which are given time-local bindings. That is,
structuring the syntax after what's used for globals,


Perl started life with nothing but dynamically scoped variables. They
added lexical scoping after they realized what a crock dynamic scoping
was.
x=10
def foo():
# No need to define x as it is only read -- same as globals
print x

def bar():
dynamic x
x = 11
foo()

def baz():
bar() # prints 11
foo() # prints 10; the binding in bar is undone when bar exits

This feature makes using "globals" sensible, providing a way to avoid
many important uses (and some say, misuses) of objects if you are so
inclined. It allows you to do some things better than objects do,
because it does to library parameters, what exceptions do to return
codes: instead of passing them in all the way from outside until a
piece of code which actually uses them, they are only mentioned where
you set them and where you really need to access them.

It would not be too hard to implement a version of this (inefficiently)
in the existing language, if frame objects could carry a modifiable
dictionary.

I suppose it is not in Python because (most) Pythoners are not looking
(hard enough) for alternatives to OOP.


Last time I checked, dynamic binding variables were frowned on in LISP
systems as well. Scheme doesn't have them. Common LISP requires
special forms to use them.

The problem with the given use case is that it lets every routine in
the call chain substitute it's own variable for the library parameter
you want to use, with no local indication that this is going
on. This makes bugs in dynamically scoped variables a PITA to find.

Given that it's a feature I don't want programmers using, I'd only be
willing to see it added to the language if you can show that it has no
overhead so long as you don't use it. I'm not sure that can be done.

<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 #116
My personal favorite would be ruby's iterators and blocks.
Instead of writing a bunch of repetitive list comprehensions or
defining a bunch of utility functions, you just use the iterators
supported by container objects.
For instance,

[f(x) for x in y]

could be written in Ruby as

y.collect |x| do
#body of f
end

You don't have to use a lambda or define f() externally.
Best of all, Ruby's containers come with many iterators for common
cases builtin.

Joseph Garvin wrote:
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 #117
[Lots of quoted text left in...]

I started thinking about this, and realized that there was a way to do
what you wanted, with no execution time overhead, and without
providing ways to radically change the program behavior behind the
scenes.

Mike Meyer <mw*@mired.or g> writes:
"Shai" <sh**@platonix. com> writes:
Joseph Garvin wrote:

I'm curious -- what is everyone's favorite trick from a non-python
language? And -- why isn't it in Python?
1. Lisp's "dynamicall y scoped" variables (Perl has them, and calls them
"local", but as far as I've seen their use their is discouraged). These
are global variables which are given time-local bindings. That is,
structuring the syntax after what's used for globals,


Perl started life with nothing but dynamically scoped variables. They
added lexical scoping after they realized what a crock dynamic scoping
was.


That's a bit harsher than I intended. I mean that having nothing but
dynamically scoped variables - like early Perl and LISP - is a
crock. I'll add that dynamic scoping as the default is a crock. But
you aren't asking for those.
x=10
def foo():
# No need to define x as it is only read -- same as globals
print x

def bar():
dynamic x
x = 11
foo()

def baz():
bar() # prints 11
foo() # prints 10; the binding in bar is undone when bar exits


Here's the problem with that. Consider this script:

import foo
x = 10
def bar():
print x

foo.foogle(bar)

If foo.foogle includes "dynamic x" and then invokes bar, bar could
print anything. This makes the behavior of bar unpredictable by
examining the sourc, with no hint that that is going on.
Given that it's a feature I don't want programmers using, I'd only be
willing to see it added to the language if you can show that it has no
overhead so long as you don't use it. I'm not sure that can be done.


Here's a proposal for dynamically bound variables that you should be
able to implement without affecting the runtime behavior of code that
doesn't use it.

Instead of dynamic meaning "all references to the named variable(s)
will be dynamic until this function exits", have it mean "the named
variable(s) will be dynamic in this function." Whether it should only
check local variables in the calling routines, check local + global,
or check for all free variables, is an open question.

I.e. - your example would be written:

x = 10
def foo():
dynamic x
print x

def bar():
x = 11
foo()

def baz():
bar() # prints 11
foo() # Possibly an error?

For my example above, bar would *always* print 10. Nothing that
foo.foogle did would change that. However, you could write:

import foo
def bar():
dynamic x
print x

foo.foogle(bar)

In this case, bar will print whatever foo.foogle sets x to - and it's
noted in the source to bar. This means that functions that don't
declare a dynamic variable can be compiled to the same code they are
compiled to now.

Would this version provide the functionality you wanted?

<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 #118
I like C++ templates so that you can ensure that a list only contain
items of one type. I also like the JMP instruction in x86 assembler,
you could do some nasty tricks with that.

--
mvh Björn
Jul 19 '05 #119

"Devan L" <de****@gmail.c om> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
With the exception of reduce(lambda x,y:x*y, sequence), reduce can be
replaced with sum, and Guido wants to add a product function.


The update function is not at all limited to sums and products, but can be
any callable with the appropriate signature. The new any() and all()
functions are examples that use other updates.

Terry J. Reedy

Jul 19 '05 #120

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
9423
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10045
jinu1996
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
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,...
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...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.

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.