[Tutor] what does the @ operator mean?

2008-12-17 Thread Daniele
 From: Alan Gauld alan.ga...@btinternet.com
 Subject: Re: [Tutor] what does the @ operator mean?

Thinking it's quite funny, I'll keep on with italian words:
the @ is called chiocciola which means snail,
while # is called cancelletto which is a small gate

As you see italian words are quite close to the sign shape,
like in Denmark i guess (trunk-a for @ is fantastic! :-P )
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] what does the @ operator mean?

2008-12-17 Thread Lie Ryan
On Tue, 16 Dec 2008 01:03:55 +, Alan Gauld wrote:

 Marc Tompkins marc.tompk...@gmail.com wrote
 
 If you're just starting out in Python, decorators can be hard to get
 your head around...
 
 I've been using Python for oover 10 years and still find decorators hard
 to get my head around! :-)
 
 I confess I'm not a fan, they go against the Python spirit of explicit
 is best in my opinion. If I'm calling a function I like to know I'm
 calling a function... I know they make the code look pretty but IMHO
 they are a pain to debug and I'm never totally convinced I've got it
 exactly right.

I usually think about decorator as a tag or marker instead of function 
calling. When I put the property decorator, I'll be thinking about 
attaching a property tag to the function so the interpreter would handle 
the function as if it is a property. This way, I don't think of it as 
implicit function calling but as an explicit tag (how the tag system 
works is a detail I need not to care).

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] what does the @ operator mean?

2008-12-17 Thread Wolfram Kraus

Am 16.12.2008 02:03, Alan Gauld schrieb:


Marc Tompkins marc.tompk...@gmail.com wrote


If you're just starting out in Python, decorators can be hard to get
your head around...


I've been using Python for oover 10 years and still find decorators
hard to get my head around! :-)

I confess I'm not a fan, they go against the Python spirit of
explicit is best in my opinion. If I'm calling a function I like to
know I'm calling a function... I know they make the code look
pretty but IMHO they are a pain to debug and I'm never totally
convinced I've got it exactly right.

Alan G


Just found this via dzone.org: 
http://gumuz.nl/weblog/simple-python-decorator-classes/


Very interesting read.

HTH,
Wolfram

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] what does the @ operator mean?

2008-12-16 Thread Kent Johnson
Here is an interesting example of a decorator that munges the
bytecodes of a function to optimize it:
http://code.activestate.com/recipes/277940/

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] what does the @ operator mean?

2008-12-16 Thread Paul McGuire
The Python Wiki has some example decorators at
http://wiki.python.org/moin/PythonDecoratorLibrary.  I think the CGIMethod
wrapper is a good intuitive example, and memoize is a good technique to add
to your Python toolkit.

-- Paul
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] what does the @ operator mean?

2008-12-16 Thread Kent Johnson
On Mon, Dec 15, 2008 at 9:33 PM, Marc Tompkins marc.tompk...@gmail.com wrote:

 I'm sorry I left it as flat as I did - that Dr. Dobbs article is a
 pretty good explanation, and there's an article somewhere in the
 Effbot bookshelf that does a decent job - but I couldn't find either
 one at that moment.

FWIW my attempt at explaining decorators is here:
http://personalpages.tds.net/~kent37/kk/1.html

 I've only been using Python for a couple of years now, but my
 experience so far is the same as yours: decorators make my head hurt.
 Even in the Dr. Dobbs article, the examples seem horribly contrived.

It's hard to find a simple example of decorators that is not
contrived. The real-world cases are often too complex for an
introduction. I cite a few examples in my article. Some other
examples:
Django's login_required decorator:
http://docs.djangoproject.com/en/dev/topics/auth/#the-login-required-decorator

In the standard lib, contextlib.contextmanager makes it easy to define
context managers (objects that work with the 'with' statement):
http://docs.python.org/library/contextlib.html#contextlib.contextmanager

and functools.wraps helps to make well-behaved decorators ;-)
http://docs.python.org/library/functools.html#functools.wraps

I told you it was hard to find simple examples!)

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] what does the @ operator mean?

2008-12-16 Thread Marc Tompkins
OK, having looked at
http://wiki.python.org/moin/PythonDecoratorLibrary again with a less
jaundiced eye, I believe my code may soon begin to contain a few of
the dreaded sleepycats...

By the way, (totally off-topic, of course, my apologies): what do all
y'all call the @ operator?  Here in the States, we call it the
at-sign, which I find boring; I believe sleepycat is a
Scandinavian thing (I picked it up in some long-forgotten article);
some Russians refer to it as собака (sobaka, dog) - again because it
looks like an animal with its tail curled up.

It occurs to me that Pythonistas should have their own name for the
thing.  I propose ourobouros - the snake eating its own tail. Of
course, technically that's a capital O, but hey.

-- 
www.fsrtechnologies.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] what does the @ operator mean?

2008-12-16 Thread Alan Gauld


Marc Tompkins marc.tompk...@gmail.com wrote 


y'all call the @ operator?  Here in the States, we call it the
at-sign, 


Thats what I've always heard in the UK too.

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] what does the @ operator mean?

2008-12-16 Thread Patrick Mullen
On Mon, Dec 15, 2008 at 5:03 PM, Alan Gauld alan.ga...@btinternet.com wrote:

 Marc Tompkins marc.tompk...@gmail.com wrote

 If you're just starting out in Python, decorators can be hard to get
 your head around...

 I've been using Python for oover 10 years and still find decorators
 hard to get my head around! :-)

 I confess I'm not a fan, they go against the Python spirit of
 explicit is best in my opinion. If I'm calling a function I like to
 know I'm calling a function... I know they make the code look
 pretty but IMHO they are a pain to debug and I'm never totally
 convinced I've got it exactly right.

I thought this way for a while, but I get them now.  Mostly I use
them as a shortcut to setting some variables on a function, which
could later be used for introspection.  For instance, a dead simple
test framework I wrote uses decorators like this:

@test(Just load something,removefiles=[sprite1.txt],teardown=sprite_cleanup)
def load_sprite():
f = open(sprite1.txt,w)
f.write(texture metatex1.png
horizontal 2\nvertical 2\nlength 4\nloops 3)
f.close()

The decorator signifies to the testing framework that this function should
be run, the file sprite1.txt should be removed after running the test, and
the function sprite_cleanup should be run as well.  This could have been
done in other ways, but it is a lot more clear with a decorator.
Hey testing framework, here is how you should run this function

The actual code for the decorator is not complex either:
def test(t,removefiles=[],teardown=None):
def dec(f):
f.a_test = True
f.test_desc = t
f.removefiles = removefiles
f.teardown = teardown
return f
return dec

The function in a function is a bit headache inducing, I'll grant that.  But the
dec function just sets some variables on the function.  This could be done
in the old way:

def test(f,t,removefiles=[],teardown=None):
f.a_test = True
f.test_desc = t

With the functions decorated like this:
def test_sprites:
[code]
test(test_sprites,Just load
something,removefiles=[sprite1.txt],teardown=sprite_cleanup)

For me though, it is MUCH better to have this information before the
function instead of after.


Another place I am using them is in an interpreter class.  The class
has various methods
tied to different commands that it understands.  I also have a gui
where commands
can be chosen, according to the category of the command.  The
decorator is used to
add some information that the gui can use about the nature of the
command, and which
category it falls under.

I don't used any advanced features of decorators at all, they just
make the code a little
bit more clear and allow me to do a bit more introspection of
functions for various purposes.
I don't use them often, but I do like them.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] what does the @ operator mean?

2008-12-16 Thread Paul McGuire

 By the way, (totally off-topic, of course, my apologies): what do all
 y'all call the @ operator?

Back when the what syntax should we use for decorators? debate raged, this
symbol was referred to as a pie, I guess because it looks like the swirl
on top of a cream pie.  I think this term is of Perl/Unix-y origin, similar
to bang(!), splat(*), and hash(#).  

I much prefer the spidermonkey or sleepycat terms, though.
Unfortunately, here in the US, far fewer would understand what I was saying
than if I called it the prosaic at-sign (so named because on old-style
store receipts, one might find 3 apples @ 5 cents, meaning at the unit
price of or just at for short).

Use of dot and star for . and * are fairly commonplace now, in place
of their older forms period and asterisk.  Did you ever hear anyone say
go to 'www period google period com'?  But it is interesting that the
clunky ampersand persists.

Perhaps as we move forward into the unicode world, we will have to have more
names for symbols like § and ¶.  My Windows character map names these
(when mousing over them) as the section sign and Pilcrow sign.  @ is
referred to here as the Commercial At, and # as the Number sign,
although all of us have probably been instructed by voice mail menus to
press the pound key when they mean this sign.  Much more good info on all
this trivia at http://en.wikipedia.org/wiki/Punctuation.

-- Paul
Mary love to wear her skates/Upon the ice to frisk/Wasn't she a silly
girl/Her little * ?


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] what does the @ operator mean?

2008-12-16 Thread spir
I use decorators only to declare class methods.
Denis

Le lundi 15 décembre 2008 à 18:33 -0800, Marc Tompkins a écrit :

 Does anybody who reads this list use decorators and have a nice word
 to say about them?  I'd be interested to hear it.
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] what does the @ operator mean?

2008-12-16 Thread Ole Henning Jensen

Marc Tompkins wrote:

By the way, (totally off-topic, of course, my apologies): what do all
y'all call the @ operator?  Here in the States, we call it the
at-sign, which I find boring; I believe sleepycat is a
Scandinavian thing (I picked it up in some long-forgotten article)


Continuing of the OT lane
I'm not sure that sleepycat is scandinavien, I'm Danish myself and to 
my knowledge in both Denmark and Sweden the at-sign is called snabel-a 
which translates to trunk-a (a trunk as on an elefant)

And in Norwegian I belive to be krølle-alfa translated means curly-alpha

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] what does the @ operator mean?

2008-12-15 Thread Mary Lou Knack
I'm looking at some code from Enthought's ETS examples and ran across the 
following statement:

@mayavi2.standalone

I have no idea what the @ operator means, if anything.  I tried searching for 
it in the documentation (locally and online), but no luck.  Or rather, the 
local (windows) help said it couldn't search for that phrase (@) and the online 
search turned up a bazillion hits and I don't know how to refine the search.

Knowledge isn't necessary for me to proceed, but it sure would be nice.  Thanks.

Mary Lou Knack___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] what does the @ operator mean?

2008-12-15 Thread Marc Tompkins
On Mon, Dec 15, 2008 at 12:33 PM, Mary Lou Knack mlkn...@gmail.com wrote:
 I'm looking at some code from Enthought's ETS examples and ran across the
 following statement:

 @mayavi2.standalone

 I have no idea what the @ operator means, if anything.  I tried searching
 for it in the documentation (locally and online), but no luck.  Or rather,
 the local (windows) help said it couldn't search for that phrase (@) and the
 online search turned up a bazillion hits and I don't know how to refine the
 search.

It indicates a decorator.
Try this:
http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Decorators

If you're just starting out in Python, decorators can be hard to get
your head around...
-- 
www.fsrtechnologies.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] what does the @ operator mean?

2008-12-15 Thread Chris Calloway

On 12/15/2008 3:42 PM, Marc Tompkins wrote:
 If you're just starting out in Python, decorators can be hard to get
 your head around...

This would be a huge help:

http://www.ddj.com/web-development/184406073

--
Sincerely,

Chris Calloway
http://www.secoora.org
office: 332 Chapman Hall   phone: (919) 599-3530
mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] what does the @ operator mean?

2008-12-15 Thread Alan Gauld


Marc Tompkins marc.tompk...@gmail.com wrote


If you're just starting out in Python, decorators can be hard to get
your head around...


I've been using Python for oover 10 years and still find decorators
hard to get my head around! :-)

I confess I'm not a fan, they go against the Python spirit of
explicit is best in my opinion. If I'm calling a function I like to
know I'm calling a function... I know they make the code look
pretty but IMHO they are a pain to debug and I'm never totally
convinced I've got it exactly right.

Alan G 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] what does the @ operator mean?

2008-12-15 Thread Mary Lou Knack

Thanks Chris  Marc

I certainly don't need to know about decorators at this stage of my 
development.  Maybe I'll be able to avoid them entirely


Mary Lou

- Original Message - 
From: Chris Calloway c...@unc.edu

To: tutor@python.org
Sent: Monday, December 15, 2008 2:22 PM
Subject: Re: [Tutor] what does the @ operator mean?



On 12/15/2008 3:42 PM, Marc Tompkins wrote:
 If you're just starting out in Python, decorators can be hard to get
 your head around...

This would be a huge help:

http://www.ddj.com/web-development/184406073

--
Sincerely,

Chris Calloway
http://www.secoora.org
office: 332 Chapman Hall   phone: (919) 599-3530
mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] what does the @ operator mean?

2008-12-15 Thread Marc Tompkins
On Mon, Dec 15, 2008 at 5:03 PM, Alan Gauld alan.ga...@btinternet.com wrote:

 Marc Tompkins marc.tompk...@gmail.com wrote

 If you're just starting out in Python, decorators can be hard to get
 your head around...

 I've been using Python for oover 10 years and still find decorators
 hard to get my head around! :-)

 I confess I'm not a fan, they go against the Python spirit of
 explicit is best in my opinion. If I'm calling a function I like to
 know I'm calling a function... I know they make the code look
 pretty but IMHO they are a pain to debug and I'm never totally
 convinced I've got it exactly right.

 Alan G

 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor


I'm sorry I left it as flat as I did - that Dr. Dobbs article is a
pretty good explanation, and there's an article somewhere in the
Effbot bookshelf that does a decent job - but I couldn't find either
one at that moment.

I've only been using Python for a couple of years now, but my
experience so far is the same as yours: decorators make my head hurt.
Even in the Dr. Dobbs article, the examples seem horribly contrived.
There must be plenty of programmers who use decorators all the time
and would feel lost without them, but I have yet to see a compelling
use case.  (It's always something like Here's another useless,
made-up situation.  We could handle it in a straightforward way, but
wouldn't it be cooler if we used a decorator instead?)

Actually, I'm being unnecessarily harsh: I can imagine a theoretical
case, where there is some operation you wish to apply to several
functions, and you don't want to write the code more than once.
(Wrapping a timer around functions comes to mind.)  But I've never run
across a situation where a decorator actually seemed like the best way
to do it, and (almost) all of the articles on the subject feel like
they were written to check an action item off the editor's to-do list:
this book won't be complete unless we mention decorators, so better
slop something together.

Does anybody who reads this list use decorators and have a nice word
to say about them?  I'd be interested to hear it.

-- 
www.fsrtechnologies.com

p.s. - Is anybody else as sick of the phrase syntactic sugar as I am?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] what does the @ operator mean?

2008-12-15 Thread Steve Willoughby

Marc Tompkins wrote:

On Mon, Dec 15, 2008 at 5:03 PM, Alan Gauld alan.ga...@btinternet.com wrote:

I've been using Python for oover 10 years and still find decorators
hard to get my head around! :-)

I've only been using Python for a couple of years now, but my
experience so far is the same as yours: decorators make my head hurt.


Interesting.  I actually rather like them and find them to be a tool to 
clean up code quite a bit.  Python's ability to have classes offer 
attributes which are really handled by wrapper functions is a similar 
animal (in the sense that they can be very nice but also could be 
horribly abused to make smelly code).


That said, I don't find myself using them too often.  Sort of like a 
special seasoning you add to the occasional dish but exotic enough to 
taste weird if you used it all the time.


For example, creating web apps in TurboGears uses decorators to expose 
methods, apply error handling and output templates without unnecessarily 
cluttering all your method definitions.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor