473,729 Members | 2,086 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,in t)
@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,in t)
@returns(float)
@classmethod #has to be last in order?
def getratio (arg1, arg2):
...
Jul 18 '05 #1
4 1399
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,in t)
@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,in t)
@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']=__decoratorbas e__.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.he re> wrote in message
news:Oo******** ************@co mcast.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,in t)
@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,in t)
@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
1570
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 mustn't know about each other. I.e., if I have a class A, and I want to add a block of functionality, I can derive it into a B that adds that fucntionality. If I want to add more functionality, I can derive B into a C. But if I want to add a...
22
2233
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 in it that needs to be addressed. (1) The test for the 'function' object needs to not test for a string but an object type instead.
3
1996
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 from Guido: "The new descriptor API makes it possible to add static methods and class methods. Static methods are easy to describe: they behave pretty much like static methods in C++ or Java." http://www.python.org/2.2.3/descrintro.html
14
2819
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 under Linux/g++ 3.3.3. --------------------------------------------- #include <iostream> #include <utility>
2
1835
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 "static"). Can someone provide an example of where you'd use these keywords? Sorry for the newbie question. Thanks.
10
3278
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 if(InvokeRequired == true) ... BeginInvoke(...) ... I have some code that ensures that an event fired from a worker (non-GUI) thread will use the appropriate BeginInvoke call for synchronisation. By offloading the synchronisation onto the worker thread the...
11
1826
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 inspecting instances to find out what capabilities they have. At the moment, I'm leaning towards a superclass (Exposed) that defines a static method which is a decorator (expose) such that any derived class can mark a method with @Exposed.expose and...
7
2280
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 "fooDecorator"
0
431
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 problem with the concept. I wish I remembered how and if LISP did it, though. Being LISP, I'd guess their implementation is also based on closures. Well, I love closures :D. And decorators.
0
8766
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
9291
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
9217
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
9158
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
8160
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...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3244
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
2698
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.