472,373 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,373 software developers and data experts.

Decorators, Identity functions and execution...

If I define a decorator like:

def t(x) :
def I(x) : return x
return I

and use it like:

@t(X)
def foo(a) :
# definition of foo...
pass

or maybe this:

@t(X)
@(Y)
def bar(a) :
# The definition of bar...
Will in encounter much of a penalty in executing
'foo' or 'bar'? If so, is there a way to define
t such that Python knows it is the identity function
and short-circuit evaluation?

Thanks in advance.
Apr 9 '06 #1
23 1679
Chance Ginger" wrote:
If I define a decorator like:

def t(x) :
def I(x) : return x
return I
.... you get a syntax error.
and use it like:

@t(X)
def foo(a) :
# definition of foo...
pass
that's also a syntax error.
or maybe this:

@t(X)
@(Y)
def bar(a) :
# The definition of bar...
and that's not even fixable.
Will in encounter much of a penalty in executing
'foo' or 'bar'?
since foo is wrapped, calling foo will call your I function, which in
turn calls the original foo.
If so, is there a way to define t such that Python knows it is
the identity function and short-circuit evaluation?


if you don't want to wrap something, don't wrap it:

def t(x) :
def I(x) :
return x
if date == friday:
return x # don't wrap it
return I # wrap it

</F>

Apr 9 '06 #2
On Sun, 09 Apr 2006 09:51:18 +0200, Fredrik Lundh wrote:
Chance Ginger" wrote:
If I define a decorator like:

def t(x) :
def I(x) : return x
return I
... you get a syntax error.


It isn't a syntax error...I tried it before I posted. In fact
def t(x) :
def I(x) : return x
return I

is correct.
and use it like:

@t(X)
def foo(a) :
# definition of foo...
pass


that's also a syntax error.


Once again this isn't an error assuming you pass in a valid 'X'.

or maybe this:

@t(X)
@(Y)
def bar(a) :
# The definition of bar...


and that's not even fixable.


Again you are mistaken. If I say:

@t(1)
@t(2)
def bar(a) : pass

It is perfectly valid.
Will in encounter much of a penalty in executing
'foo' or 'bar'?


since foo is wrapped, calling foo will call your I function, which in
turn calls the original foo.
If so, is there a way to define t such that Python knows it is
the identity function and short-circuit evaluation?


if you don't want to wrap something, don't wrap it:

def t(x) :
def I(x) :
return x
if date == friday:
return x # don't wrap it
return I # wrap it

</F>


Decorators are a way to add "syntactic" sugar to Python,
extending it in ways that make it useful for tools. What
I am trying to do is lessen the impact on the time used
in executing Python code when I use some forms of decorators.
Apr 9 '06 #3
Chance Ginger <cg********@hotmail.com> writes:
On Sun, 09 Apr 2006 09:51:18 +0200, Fredrik Lundh wrote:
Chance Ginger" wrote:
If I define a decorator like:

def t(x) :
def I(x) : return x
return I
... you get a syntax error.


It isn't a syntax error...I tried it before I posted. In fact
def t(x) :
def I(x) : return x
return I

is correct.


Indeed. This is correct. Fredrick's comment was related to the lack of
indentation in your code.
and use it like:

@t(X)
def foo(a) :
# definition of foo...
pass


that's also a syntax error.


Once again this isn't an error assuming you pass in a valid 'X'.


If your indentation is broken as above it doesn't matter what 'X' is.

--
Jorge Godoy <go***@ieee.org>

"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.
Apr 9 '06 #4
Chance Ginger wrote:
It isn't a syntax error...I tried it before I posted. In fact
def t(x) :
def I(x) : return x
return I

is correct.
tabs don't make it through all channels. don't use tabs for
indentation when you post to newsgroups or mailing lists.

and @(Y) is not valid Python syntax. no amount of indentation
will change that.
Decorators are a way to add "syntactic" sugar to Python,
extending it in ways that make it useful for tools.
that's a rather narrow view of what a decorator does, and doesn't
help much in understanding how they work.

which is unfortunate, because it's very simple: decorators are simply
ordinary callables, and the result of the decoration is whatever the
callable returns.

in fact, any callable can be used to decorate a function:
@str ... def foo(bar):
... pass
... foo '<function foo at 0x00986730>' foo("bar") Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'str' object is not callable

and it's all done at runtime; there is no magic involved whatsoever.
@open

... @str
... def bar(foo):
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 2] No such file or directory: '<function bar at 0x009867F0>'
I am trying to do is lessen the impact on the time used in
executing Python code when I use some forms of decorators.


if you don't want Python to execute some code, all you have to do is
to make sure that it isn't called.

</F>

Apr 9 '06 #5

Chance Ginger wrote:
On Sun, 09 Apr 2006 09:51:18 +0200, Fredrik Lundh wrote:
Chance Ginger" wrote:
If I define a decorator like:

def t(x) :
def I(x) : return x
return I
... you get a syntax error.


It isn't a syntax error...I tried it before I posted. In fact
def t(x) :
def I(x) : return x
return I

is correct.


You've made the unfortunate mistake of indenting it with tabs, which do
not show up on some newsreaders. I see the tabs in Google; people
using Microsoft Outlook do not.

Always use spaces when posting, and use them in your code as well.
Spaces are the current recommended practice, and in the future tabs
might become illegal. I'd prefer tabs myself, but it's more important
to respect community standards than to stick to some silly preference
you have.

Decorators are a way to add "syntactic" sugar to Python,
extending it in ways that make it useful for tools. What
I am trying to do is lessen the impact on the time used
in executing Python code when I use some forms of decorators.


One suggestion. Have you run the script, determined it's too slow, and
are trying to optimize? If not (and it doesn't sound like you are), I
suggest that it's too soon to worry about whether this decorator has
any overhead. You may end up doing a lot of work optimizing that will
ultimately have very little benefit.

Having said that, this decorator will not affect calling overhead at
all. The decorator is applied when the module is loaded, not when the
decorated function is called.
Carl Banks

Apr 9 '06 #6
First, thanks for the tip of 'tabs'. I keep forgetting
Outlook has some interesting rules about displaying text.

Thanks for the comment about happening at load time. That
resolved the problem (in my thinking)! I don't believe I
have an issue at all...

Peace,
CG.

On Sun, 09 Apr 2006 08:52:18 -0700, Carl Banks wrote:

Chance Ginger wrote:
On Sun, 09 Apr 2006 09:51:18 +0200, Fredrik Lundh wrote:
> Chance Ginger" wrote:
>
>> If I define a decorator like:
>>
>> def t(x) :
>> def I(x) : return x
>> return I
>
> ... you get a syntax error.
>


It isn't a syntax error...I tried it before I posted. In fact
def t(x) :
def I(x) : return x
return I

is correct.


You've made the unfortunate mistake of indenting it with tabs, which do
not show up on some newsreaders. I see the tabs in Google; people
using Microsoft Outlook do not.

Always use spaces when posting, and use them in your code as well.
Spaces are the current recommended practice, and in the future tabs
might become illegal. I'd prefer tabs myself, but it's more important
to respect community standards than to stick to some silly preference
you have.

Decorators are a way to add "syntactic" sugar to Python,
extending it in ways that make it useful for tools. What
I am trying to do is lessen the impact on the time used
in executing Python code when I use some forms of decorators.


One suggestion. Have you run the script, determined it's too slow, and
are trying to optimize? If not (and it doesn't sound like you are), I
suggest that it's too soon to worry about whether this decorator has
any overhead. You may end up doing a lot of work optimizing that will
ultimately have very little benefit.

Having said that, this decorator will not affect calling overhead at
all. The decorator is applied when the module is loaded, not when the
decorated function is called.
Carl Banks


Apr 9 '06 #7
Carl Banks wrote:
Having said that, this decorator will not affect calling overhead at
all. The decorator is applied when the module is loaded, not when the
decorated function is called.


to be precise, the decorator is applied when the "def" statement is exe-
cuted (that is, when the decorated function object is created).

this may be load time, or, for nested functions, some other time.

</F>

Apr 9 '06 #8
On Sun, 09 Apr 2006 08:52:18 -0700, Carl Banks wrote:
it's more important
to respect community standards than to stick to some silly preference
you have.


What happens when the community standard is a silly preference? I object
to the suggestion that "community standards" (that is, a standard not even
designed by a committee, merely evolved by a mob) is necessarily worthy of
respect.

As it turns out, regarding this particular "silly preference" (community
or private), I always remember that communication is the purpose of email
(and, believe it or not, Usenet), and the use of tabs in some buggy
news readers can cause the failure of communication. Hence I use spaces
when posting code. The rest of the time, particularly in my private
coding, my preference (whatever it is) is no more or less irrational than
the other preference.

--
Steven.

Apr 9 '06 #9
Steven D'Aprano <st***@REMOVETHIScyber.com.au> wrote:
On Sun, 09 Apr 2006 08:52:18 -0700, Carl Banks wrote:
it's more important
to respect community standards than to stick to some silly preference
you have.
What happens when the community standard is a silly preference? I object
to the suggestion that "community standards" (that is, a standard not even
designed by a committee, merely evolved by a mob) is necessarily worthy of
respect.


Design by committee has far from a stellar track record. On the Internet,
most of the really good stuff has come from the mobs.
As it turns out, regarding this particular "silly preference" (community
or private), I always remember that communication is the purpose of email
(and, believe it or not, Usenet), and the use of tabs in some buggy
news readers can cause the failure of communication. Hence I use spaces
when posting code.


As you should. It matters not whether news readers that can't handle tabs
are buggy or not. The fact is that they exist. One of the most basic
maxims on the Internet has always been, "Be liberal in what you accept, be
conservative in what you produce".
Apr 9 '06 #10
Em Dom, 2006-04-09 Ã*s 08:52 -0700, Carl Banks escreveu:
You've made the unfortunate mistake of indenting it with tabs, which
do
not show up on some newsreaders. I see the tabs in Google; people
using Microsoft Outlook do not.


He does not need to know that some poor designed newsreaders mess up
with tabs at will. Python supports tab indentation and it's not
forbidden to use it (if it was, support for it in the Python codebase
would have been removed long time ago).

I don't like to code with tab-indents, I use 4 spaces, but I don't press
space four times each time I need to indent something, I just press Tab
and the editor does the clunky part for me. The problem is that most (if
not all) mail programs/newsreaders don't convert tabs to spaces, they
don't even keep the indentation of the last line, so nobody has the
obligation of opening its IDE or hitting dozens of times the space key
just to post a message to the list.

--
Felipe.

Apr 9 '06 #11

Felipe Almeida Lessa wrote:
Em Dom, 2006-04-09 às 08:52 -0700, Carl Banks escreveu:
You've made the unfortunate mistake of indenting it with tabs, which
do
not show up on some newsreaders. I see the tabs in Google; people
using Microsoft Outlook do not.
He does not need to know that some poor designed newsreaders mess up
with tabs at will.


Yes, he does need to know if he doesn't want some people to pass over
his post.
Python supports tab indentation and it's not
forbidden to use it (if it was, support for it in the Python codebase
would have been removed long time ago).


Nope. If tab indentation were the biggest mistake in the language, it
would still be supported because of backwards compatibility. I don't
recall the status of tabs in Py3K but your reasoning is wrong.
Carl Banks

Apr 10 '06 #12

Steven D'Aprano wrote:
On Sun, 09 Apr 2006 08:52:18 -0700, Carl Banks wrote:
it's more important
to respect community standards than to stick to some silly preference
you have.


What happens when the community standard is a silly preference? I object
to the suggestion that "community standards" (that is, a standard not even
designed by a committee, merely evolved by a mob) is necessarily worthy of
respect.


It is worthy of respect because it's such a stupid thing to even have a
preference for; consistency with the community is by far the strongest
argument one could make for or against tabs.
Carl Banks

Apr 10 '06 #13
On Sun, 09 Apr 2006 11:42:34 -0300, rumours say that Jorge Godoy
<go***@ieee.org> might have written:
Indeed. This is correct. Fredrick's comment was related to the lack of
indentation in your code.


His code was indented fine, as you maybe noticed later on. The actual
problem was that he had tabs, so Fredrik's Outlook Express (and I guess
other newsreaders too) did not show indentation.

Fredrik suggested already the typical "use spaces, not tabs"; I just thought
that "lack of indentation" was unfair for the OP.
--
TZOTZIOY, I speak England very best.
"Dear Paul,
please stop spamming us."
The Corinthians
Apr 10 '06 #14
In article <11**********************@v46g2000cwv.googlegroups .com>,
"Carl Banks" <in**********@aerojockey.com> wrote:
Always use spaces when posting, and use them in your code as well.
Spaces are the current recommended practice, and in the future tabs
might become illegal. I'd prefer tabs myself, but it's more important
to respect community standards than to stick to some silly preference
you have.


Tab stops every 4 columns. What's silly about that? Any other definition
is silly, or at best antiquated.
Apr 11 '06 #15
In article <ro***********************@reader1.panix.com>,
Roy Smith <ro*@panix.com> wrote:
One of the most basic
maxims on the Internet has always been, "Be liberal in what you accept, be
conservative in what you produce".


How do you explain top-posting, then?
Apr 11 '06 #16
On Tue, 11 Apr 2006 15:05:22 +1200, rumours say that Lawrence D'Oliveiro
<ld*@geek-central.gen.new_zealand> might have written:
In article <ro***********************@reader1.panix.com>,
Roy Smith <ro*@panix.com> wrote:
One of the most basic
maxims on the Internet has always been, "Be liberal in what you accept, be
conservative in what you produce".
How do you explain top-posting, then?


“Be lazy and let your news/mail client choose for you.â€

Unless you meant “how do you explain top-posting *acceptance* by non
top-posters.â€Â* That is another branch of psychology.
--
TZOTZIOY, I speak England very best.
"Dear Paul,
please stop spamming us."
The Corinthians
Apr 11 '06 #17
Lawrence D'Oliveiro wrote:
In article <11**********************@v46g2000cwv.googlegroups .com>,
"Carl Banks" <in**********@aerojockey.com> wrote:
Always use spaces when posting, and use them in your code as well.
Spaces are the current recommended practice, and in the future tabs
might become illegal. I'd prefer tabs myself, but it's more important
to respect community standards than to stick to some silly preference
you have.


Tab stops every 4 columns. What's silly about that? Any other definition
is silly, or at best antiquated.


Every day I come across people or programs that use tab stops every 2
or 8 columns. I am another fan of tabs every 4 columns, but
unfortunately this isn't standard, so spaces in Python it is.

--
Ben Sizer

Apr 11 '06 #18
Ben Sizer enlightened us with:
Every day I come across people or programs that use tab stops every
2 or 8 columns. I am another fan of tabs every 4 columns, but
unfortunately this isn't standard, so spaces in Python it is.


I don't care about how people see my tabs. I use one tab for every
indent level, so no matter how you set your tab width, my code will
look consistent.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Apr 11 '06 #19

"Sybren Stuvel" <sy*******@YOURthirdtower.com.imagination> wrote in
I don't care about how people see my tabs. I use one tab for every
indent level, so no matter how you set your tab width, my code will
look consistent.


Unless they view with tab_width = 0, as with some news readers.

tjr


Apr 12 '06 #20
In article <11**********************@t31g2000cwb.googlegroups .com>,
"Ben Sizer" <ky*****@gmail.com> wrote:
Lawrence D'Oliveiro wrote:
In article <11**********************@v46g2000cwv.googlegroups .com>,
"Carl Banks" <in**********@aerojockey.com> wrote:
>Always use spaces when posting, and use them in your code as well.
>Spaces are the current recommended practice, and in the future tabs
>might become illegal. I'd prefer tabs myself, but it's more important
>to respect community standards than to stick to some silly preference
>you have.


Tab stops every 4 columns. What's silly about that? Any other definition
is silly, or at best antiquated.


Every day I come across people or programs that use tab stops every 2
or 8 columns. I am another fan of tabs every 4 columns, but
unfortunately this isn't standard, so spaces in Python it is.


All we have to do is keep using and promoting 4-column tabs every chance
we get. I've been using them myself for about 18 years. Eventually
they'll become accepted as the standard.
Apr 12 '06 #21
In article <ma***************************************@python. org>,
"Terry Reedy" <tj*****@udel.edu> wrote:
"Sybren Stuvel" <sy*******@YOURthirdtower.com.imagination> wrote in
I don't care about how people see my tabs. I use one tab for every
indent level, so no matter how you set your tab width, my code will
look consistent.


Unless they view with tab_width = 0, as with some news readers.


I have my newsreader set to use a proportional font, so the point is
moot.

Any time I want to see something in fixed columns, I copy and paste it
into an editor window I always happen to have handy.
Apr 12 '06 #22
Lawrence D'Oliveiro wrote:
In article <11**********************@t31g2000cwb.googlegroups .com>,
"Ben Sizer" <ky*****@gmail.com> wrote:
Every day I come across people or programs that use tab stops every 2
or 8 columns. I am another fan of tabs every 4 columns, but
unfortunately this isn't standard, so spaces in Python it is.


All we have to do is keep using and promoting 4-column tabs every chance
we get. I've been using them myself for about 18 years. Eventually
they'll become accepted as the standard.


"I am Lawrence of Borg. You will be tabulated. Resistance is futile."

:-)

Apr 12 '06 #23
On Wed, 12 Apr 2006 17:19:25 +1200 in comp.lang.python, Lawrence
D'Oliveiro <ld*@geek-central.gen.new_zealand> wrote:
In article <ma***************************************@python. org>,
"Terry Reedy" <tj*****@udel.edu> wrote:
"Sybren Stuvel" <sy*******@YOURthirdtower.com.imagination> wrote in
> I don't care about how people see my tabs. I use one tab for every
indent level, so no matter how you set your tab width, my code will
look consistent.


Unless they view with tab_width = 0, as with some news readers.


I have my newsreader set to use a proportional font, so the point is
moot.


Even in a proportional font, spaces all have the same width. Though
not as wide as a fixed font...

Regards,
-=Dave

--
Change is inevitable, progress is not.
Apr 12 '06 #24

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

Similar topics

17
by: daishi | last post by:
For what it's worth: As far as I know, the proposed @decorator syntax will be the first time that two logical lines of python with the same indentation will not be independent of one another....
8
by: Michele Simionato | last post by:
Decorators can generate endless debate about syntax, but can also be put to better use ;) Actually I was waiting for decorators to play a few tricks that were syntactically too ugly to be even...
0
by: Bengt Richter | last post by:
Still looking for a unifying concept ;-) It struck me that @deco1 @deco2 def foo():pass uses '@' to register at-next-name-binding processing in a way similar to atexit's registering of...
0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
13
by: vinjvinj | last post by:
I'm building an application with cherrypy and have started using decorators quite extensively. A lot of my exposed functions look like: @expose @startTransactrionAndBuildPage...
10
by: Jerry | last post by:
I have just started to do some semi-serious programming (not one-off specialized scripts) and am loving Python. I just seem to get most of the concepts, the structure, and the classes (something I...
11
by: Helmut Jarausch | last post by:
Hi, are decorators more than just syntactic sugar in python 2.x and what about python 3k ? How can I find out the predefined decorators? Many thanks for your help, Helmut Jarausch
2
by: Andrew West | last post by:
Probably a bit of weird question. I realise decorators shouldn't be executed until the function they are defined with are called, but is there anyway for me to find all the decorates declared in a...
0
by: Gabriel Genellina | last post by:
En Tue, 29 Jul 2008 08:45:02 -0300, Themis Bourdenas <bourdenas@gmail.com> escribi�: In a very strict sense, I'd say that all those references to "method decorators" are wrong - because...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.