473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Rather than decorators, how about sections?

I like many am not wild about the <at> operator. I also don't think
that the decorator syntax should be so directly attached to the method,
since what we're trying to do is to say something about the
*relationship between* a method and a class (e.g. "method m is a
staticmethod of class C").

So if we are going to extend the Python grammar to support this sort of
thing (which I believe is a good idea), my preference would be to
introduce named sections within a class definition, such as...

class Foo(object):
staticmethods:
def baz(a,b):
print "I'm a static method."
def bez(c,d):
print "I'm a static method too."

classmethods:
def biz(klass):
print "I'm a class method."
def __init__(self):
print "We all know what I am."
Jul 18 '05 #1
8 1554
"Paul Morrow" <pm****@yahoo.com> wrote in message
news:ma**************************************@pyth on.org...
I like many am not wild about the <at> operator. I also don't think
that the decorator syntax should be so directly attached to the method,
since what we're trying to do is to say something about the
*relationship between* a method and a class (e.g. "method m is a
staticmethod of class C").

So if we are going to extend the Python grammar to support this sort of
thing (which I believe is a good idea), my preference would be to
introduce named sections within a class definition, such as...

class Foo(object):
staticmethods:
def baz(a,b):
print "I'm a static method."
def bez(c,d):
print "I'm a static method too."

classmethods:
def biz(klass):
print "I'm a class method."
def __init__(self):
print "We all know what I am."

This only addresses the "decoration" for declaring static and class methods.
The decorator mechanism is intended to include much more than this simple
class declaration feature. See
http://www.python.org/cgi-bin/moinmo...coratorLibrary for the first
application (memoize - a return value cacheing helper/decorator, to optimize
access to repetitive or compute-intensive function calls, with NO changes
needed to the function itself). Other decorator candidates I've seen
mentioned are:
- mutex lock/unlock wrapper
- debugging/logging wrapper
- pre-condition/post-condition assertion wrappers
- input argument validation/typing wrapper
- return value type validation wrapper

For this degree of flexibility, you can't hard-wire in specific section
names.

-- Paul
Jul 18 '05 #2
Paul McGuire wrote:
"Paul Morrow" <pm****@yahoo.com> wrote in message
news:ma**************************************@pyth on.org...
I like many am not wild about the <at> operator. I also don't think
that the decorator syntax should be so directly attached to the method,
since what we're trying to do is to say something about the
*relationship between* a method and a class (e.g. "method m is a
staticmethod of class C").

So if we are going to extend the Python grammar to support this sort of
thing (which I believe is a good idea), my preference would be to
introduce named sections within a class definition, such as...

class Foo(object):
staticmethods:
def baz(a,b):
print "I'm a static method."
def bez(c,d):
print "I'm a static method too."

classmethods:
def biz(klass):
print "I'm a class method."
def __init__(self):
print "We all know what I am."


This only addresses the "decoration" for declaring static and class methods.
The decorator mechanism is intended to include much more than this simple
class declaration feature. See
http://www.python.org/cgi-bin/moinmo...coratorLibrary for the first
application (memoize - a return value cacheing helper/decorator, to optimize
access to repetitive or compute-intensive function calls, with NO changes
needed to the function itself). Other decorator candidates I've seen
mentioned are:
- mutex lock/unlock wrapper
- debugging/logging wrapper
- pre-condition/post-condition assertion wrappers
- input argument validation/typing wrapper
- return value type validation wrapper

For this degree of flexibility, you can't hard-wire in specific section
names.

-- Paul


I agree. My proposal wasn't looking at the bigger picture here. But
see the post by Stepfan Eischet in this thread. It doesn't address all
of those issues either, but it does make the bigger problem smaller, by
using an obvious interpretation of the method arguments to infer the
type of method (i.e. class -versus- static -versus- instance). By using
this simple convention (that most everyone is probably using already),
that aspect of the annotations require no additional syntax.

Hmmm... did I say 'aspect'? Maybe instead of cluttering up the code
with all of these 'declarations' (which is clearly where the decoration
movement is headed), we should have some sort of companion file that
contains these annotations, similar to what you see in the AOP languages.

Jul 18 '05 #3
Paul McGuire wrote:
This only addresses the "decoration" for declaring static and class methods.
The decorator mechanism is intended to include much more than this simple
class declaration feature. See
http://www.python.org/cgi-bin/moinmo...coratorLibrary for the first
application (memoize - a return value cacheing helper/decorator, to optimize
access to repetitive or compute-intensive function calls, with NO changes
needed to the function itself). Other decorator candidates I've seen
mentioned are:
- mutex lock/unlock wrapper
- debugging/logging wrapper
- pre-condition/post-condition assertion wrappers
- input argument validation/typing wrapper
- return value type validation wrapper > needed to the function itself).


memoize is an example of something that probably should not be specified
in the method definition. It's strictly a 'pragma' --- just a
suggestion to the compiler on how it could speed up the function. That
kind of 'decoration' (as well as some of the other examples you cited)
is of a 2nd class nature in that it doesn't affect whether the program
produces the correct result or not. Therefore, I would not want to have
such statements comingled with the statements that truly are germane to
the correct functioning of my code. They would only be a distraction,
and are best relegated to some other place in the code-base (another
file perhaps).

That's kindof the thinking behind aspect oriented programming (AOP). A
body of code should deal with only one aspect of the problem...
Jul 18 '05 #4

May I point out that the term "static" comes from its use in a particular language and is used for two semantically different purposes. I don't think the word reveals the meaning very well. I strongly suggest to use a different term, that describes better what it does.

some ideas:

instance - class
noninheritable - inheritable
specific - generic
this - all
(self, var) - (-, var)

- Josef
Jul 18 '05 #5
Paul Morrow wrote:
class Foo(object):
staticmethods:
def baz(a,b):
print "I'm a static method."
def bez(c,d):
print "I'm a static method too."

classmethods:
def biz(klass):
print "I'm a class method."
def __init__(self):
print "We all know what I am."
I don't.

Is there a simple primer for static and class methods
and decorators and such that you can recommend?

I've looked at the URL you gave with the Memoize class
http://www.python.org/cgi-bin/moinmo...coratorLibrary,
but it assumes you already know what decorators are.

Then, later, Paul Morrow wrote:
Fine, instead of Foo (the name of the class), use the > word klass. It's a popular name for the first parameter of a class method.


"klass" isn't a word. It is a twee deliberate
mispelling of a word.

It also runs the risk of causing confusion when code is
spoken aloud (eg in actual human-to-human communication
using alternating high and low pressure atmospheric
waves to deliver packets of information, or in text
readers for the blind).

It is bad enough when coders (including myself) choose
to deliberately mispell words, but to make it required
is unforgivable.
--
Steven.
Any mispellings in the above post are deliberate, and
are there to show the reader wonderful new ways of
spelling old words.
Jul 18 '05 #6

"Steve" <di****@yahoo.com> wrote in message
news:41**************@yahoo.com...

Is there a simple primer for static and class methods
and decorators and such that you can recommend?


It's cleverly hidden on the Python documentation
page, under "additional documentation."

http://www.python.org/doc/newstyle.html

At some point all of this should be integrated into
the standard documentation, but that task has been
hanging fire since 2.2.

John Roth
Jul 18 '05 #7
[Bystander delurking -- idle musings...]

[Paul Morrow wrote:]
Hmmm... did I say 'aspect'? Maybe instead of cluttering up the code
with all of these 'declarations' (which is clearly where the decoration
movement is headed), we should have some sort of companion file that
contains these annotations, similar to what you see in the AOP languages.


Splork!

-- And just yesterday, continuing to note all the discussion in
py-dev (and here) involving just where in the *linear* layout of
the code the decorators should go, I thought that maybe we need
tables -- two-dimensional coding (orthogonal beats flat)...?

1) A column of cells (of lines, blocks?) for what we now use as
our single, linear arrangement (lines) of code.

2) A column of cells for decorators and the like -- decorators
would go in the cells alongside the decorated code.

3) Perhaps a column for source code documentation.

4) [...]

(Of course, then we'd need new editors, etc.)

;-)

thinking-laterally-y'rs,
Richard

--
R Hanson [The mangled email addie below works.]
sick<P0INT>ole<PERI0D>fart<PIE_DEC0_SYNTAX>newsguy <MARK>com
Jul 18 '05 #8
Steve wrote:
Then, later, Paul Morrow wrote:
> Fine, instead of Foo (the name of the class), use the > word klass.

It's a popular name for the first
> parameter of a class method.


"klass" isn't a word. It is a twee deliberate mispelling of a word.

It also runs the risk of causing confusion when code is spoken aloud (eg
in actual human-to-human communication using alternating high and low
pressure atmospheric waves to deliver packets of information, or in text
readers for the blind).

It is bad enough when coders (including myself) choose to deliberately
mispell words, but to make it required is unforgivable.


Ok I believe that's one vote against 'klass' as a reserved word :-)

Then let it be 'cls', which is also popular. Or heck, to be flexible,
let it be something you can specify (as an environment variable,
compiler setting, etc.). The important point is that we should resist
changes to the language that require that the developer specify things
that are already clearly inferred thru the form/structure of his/her
code. We don't have to specify (with curly braces or whatever) where
code blocks start and end, because the form of our code tells us that.
It seems natural (to me, and at least one other person) to let the form
of a function's formal parameter list designate the type of function:

- if the 1st parameter is named 'self', it's an instance method.
- if the 1st parameter is 'cls' (or perhaps some suitable synonym),
it's a class method.
- for all other parameter arrangements, including no parameters, it's
a static method.

Simple, clear, obvious, intuitive, and I'm sorry, but this is the least
ugly of all the alternatives. There is nothing beautiful about at
signs, superflous code, clutter...

Jul 18 '05 #9

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

Similar topics

4
by: Michael Sparks | last post by:
Anyway... At Europython Guido discussed with everyone the outstanding issue with decorators and there was a clear majority in favour of having them, which was good. From where I was sitting it...
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....
4
by: Doug Holton | last post by:
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...
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...
2
by: Guido van Rossum | last post by:
Robert and Python-dev, I've read the J2 proposal up and down several times, pondered all the issues, and slept on it for a night, and I still don't like it enough to accept it. The only reason...
4
by: John Henry | last post by:
I must be very thick. I keep reading about what decorators are and I still don't have a good feel about it. See, for example: http://alex.dojotoolkit.org/?p=564 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...
10
by: David C. Ullrich | last post by:
What version added decorators (using the @decorator syntax)? (Is there a general way I could have found out the answer myself?) Is there a somthing such that "from __future__ import something"...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.