472,780 Members | 2,264 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,780 software developers and data experts.

compromise? keywords for static/class, move decorators to top offunction

First let me say please see the wiki page about python decorators if you
haven't already: http://www.python.org/cgi-bin/moinmoin/PythonDecorators

I propose (and others have) that built-in features have keyword support,
like static and class methods. Also, I believe it is more readable if
decorators, especially longer ones, are moved to the top of the function
body, just like docstrings, instead of coming before the function is
even declared. Whether you use @ or [] is still open.

def classmethod getratio (arg1, arg2):
@accepts(int,int)
@returns(float)
...

def classmethod getratio (arg1, arg2):
[accepts(int,int), returns(float)]
...

This has these advantages: the function declaration itself is still the
first and most important thing, decorators are indented just like the
body of the function so it is more clearly a part of the function.

In the future though, if you add an "as" keyword for adapters, you could
just say:

def classmethod getratio (arg1 as int, arg2 as int) as float:
...

contrast that simple example with this, which is kind of ugly:

@accepts(int,int)
@returns(float)
@classmethod #has to be last in order?
def getratio (arg1, arg2):
...
Jul 18 '05 #1
4 1351
On Friday 06 August 2004 12:54, Doug Holton wrote:
I propose (and others have) that built-in features have keyword
support, like static and class methods. Also, I believe it is more
readable if decorators, especially longer ones, are moved to the top
of the function body, just like docstrings, instead of coming before
the function is even declared. Whether you use @ or [] is still
open.

def classmethod getratio (arg1, arg2):
@accepts(int,int)
@returns(float)
...


Ooh... I like, I like...

This seems to be the most reasonable, readable proposition I've seen so
far. And I'd favor @ for this - it makes it clear that this is
"something different".

-Michael
Jul 18 '05 #2
On Fri, 6 Aug 2004 13:15:52 -0500, Michael Ekstrand
<py****@elehack.net> wrote:
On Friday 06 August 2004 12:54, Doug Holton wrote:
I propose (and others have) that built-in features have keyword
support, like static and class methods. Also, I believe it is more
readable if decorators, especially longer ones, are moved to the top
of the function body, just like docstrings, instead of coming before
the function is even declared. Whether you use @ or [] is still
open.

def classmethod getratio (arg1, arg2):
@accepts(int,int)
@returns(float)
...


Ooh... I like, I like...

This seems to be the most reasonable, readable proposition I've seen so
far. And I'd favor @ for this - it makes it clear that this is
"something different".


It seems to me that Python is at its best when the naive approach, and
the schooled approach, coalesce.

I can only speak for the naive approach.

And - within the context of Python as it is -the possiblity of
requiring the placement of essential information related to defining a
method amd its behavior somewhere outside the method's body would
never occur to me.

Doesn't readibility require that you have the ability to move in a
direction down the page, and, if so, why am I getting information
about a method prior to the existence of the method, before a proper
introduction has been made, i.e. before I even know its name.

In short, I would agree with both points, built-in support to
disambiguate certain common cases from the case of something truly
meta-like and implementation specific going down, and the balance in
the method body.

Just repeating a thought - but it seems unnatural to be introduced to
lots of other information about something, before the normal
forrmalities have been attended to. Name please.

Art
Jul 18 '05 #3
There are a lot of people talking about decorators
that I'm certain are a LOT smarter than me but here
goes:

Couldn't we use a dictionary construct like the one
used in classes to hold the dictionaries attributes.
I'm talking about __dict__ construct.

def getratio(arg1, arg2):
__decorators__['getratio']={} # Maybe this is automatic?
# or __decorators__['getratio']=__decoratorbase__.copy()
__decorators__['getratio']['classmethod']=True
__decorators__['getratio']['accepts']=(int, int)
__decorators__['getratio']['returns']=(float)
__decorators__['getratio']['metadata1']="some metadata"
...

I'm sure I'm missing something but this seems to
be 1) Easy on my eye, 2) Extensible (any metadata
that you want), 3) Consistent with something I'm
already comfortable with.

I'm certain that I'm missing something very important, but
I have to agree with many others, the '@' syntax just
doesn't feel correct.

Regards,
Larry Bates
Syscon, Inc.

"Doug Holton" <in****@spam.here> wrote in message
news:Oo********************@comcast.com...
First let me say please see the wiki page about python decorators if you
haven't already: http://www.python.org/cgi-bin/moinmoin/PythonDecorators

I propose (and others have) that built-in features have keyword support,
like static and class methods. Also, I believe it is more readable if
decorators, especially longer ones, are moved to the top of the function
body, just like docstrings, instead of coming before the function is
even declared. Whether you use @ or [] is still open.

def classmethod getratio (arg1, arg2):
@accepts(int,int)
@returns(float)
...

def classmethod getratio (arg1, arg2):
[accepts(int,int), returns(float)]
...

This has these advantages: the function declaration itself is still the
first and most important thing, decorators are indented just like the
body of the function so it is more clearly a part of the function.

In the future though, if you add an "as" keyword for adapters, you could
just say:

def classmethod getratio (arg1 as int, arg2 as int) as float:
...

contrast that simple example with this, which is kind of ugly:

@accepts(int,int)
@returns(float)
@classmethod #has to be last in order?
def getratio (arg1, arg2):
...

Jul 18 '05 #4
Larry Bates wrote:
Couldn't we use a dictionary construct like the one
used in classes to hold the dictionaries attributes.
I'm talking about __dict__ construct.

def getratio(arg1, arg2):
__decorators__['getratio']={} # Maybe this is automatic?
(...)
I'm certain that I'm missing something very important,


Yes: Decorators should be set before the function is called. Your
code is executed after function is called. Or if you define some
magic to avoid that, it still looks like it is.

--
Hallvard
Jul 18 '05 #5

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

Similar topics

12
by: Humpty Dumpty | last post by:
Hello, I'm experimenting with different ways of extending a class (for a plug-ins framework for a GUI) with more than one extension when some of these extensions need to collaborate, but others...
22
by: Ron_Adam | last post by:
Hi, Thanks again for all the helping me understand the details of decorators. I put together a class to create decorators that could make them a lot easier to use. It still has a few glitches...
3
by: Steven D'Aprano | last post by:
I've been doing a lot of reading about static methods in Python, and I'm not exactly sure what they are useful for or why they were introduced. Here is a typical description of them, this one...
14
by: John Ratliff | last post by:
I'm trying to find out whether g++ has a bug or not. Wait, don't leave, it's a standard C++ question, I promise. This program will compile and link fine under mingw/g++ 3.4.2, but fails to link...
2
by: dw | last post by:
Hi, everyone. I'm having difficulty understanding the difference of the keywords "friend" and "protected" vs. some of the other ways you can declare variables in ASP.NET ("dim", "public", and...
10
by: Jon | last post by:
I'm investiging multi-threaded GUI applications; specifically the technique used to ensure that controls are only modified under the correct thread. The standard technique is to use ...
11
by: glen.coates.bigworld | last post by:
I'm developing a library at the moment that involves many classes, some of which have "exposed" capabilities. I'm trying to design a nice interface for both exposing those capabilities, and...
7
by: MR | last post by:
Hello All, I have a question about decorators, and I think an illustration would be helpful. Consider the following simple class: #begin code class Foo: def fooDecorator(f): print...
0
by: Luis Zarrabeitia | last post by:
Quoting Joe Strout <joe@strout.net>: But, at least in C++, its really confusing. The 'static' word is reservedon C++ for more than one meaning, but I'll agree that it is a C++ problem and not a...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.