473,406 Members | 2,954 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,406 software developers and data experts.

Argument Decorators Enhancement?

Guido has proposed a syntax for type annotations in Python-3000.
Example:

def foo(x: t1, y: t2) -> t3:
...body...
http://www.artima.com/weblogs/viewpost.jsp?thread=87182

The types are dynamic and there is significant execution of code prior
to the function body being called. Which tends to make the above closer
to a decorator than a static type declaration. That being so, it
occurred to me that perhaps an argument decorator would be more general
and perhaps less restrictive. The example using decorators:

def foo(@T1 x, @t2 y) @ t3:
...body...

The compiler would do something like this:

def foo__(x, y): # original function
...body...

def foo(x, y): # wrapper function
x = T1.__decarg__(foo_, x)
y = t2.__decarg__(foo_, y)
r = foo__(x, y)
return t3.__decreturn___(foo__, r)

More examples:

def func(@between(1,400) x) @bool : # between checks input range

def func(@NotNone x): # x must be a real object

def func(@long x) @coerce(float): # coerce forces type conversion

def func(@sequence s, @function fn) @sequence: # more abstract types

To someone used to reading decorators, the @ character is a flag that
some kind of dynamic magic is being run. So IMHO using @ here too is
consistent with its existing usage.

Argument decorators would be an object (not a function) with methods
corresponding to their usage:

class T1(object):
def __decarg__(self, function, arg): # returns arg
def __decreturn__(self, function, arg): # returns arg

This will allow existing types to be used if they implement these
methods:

def bar(@int x, @float y) @float :
...body...

Because this syntax is just a decorator, you could use or abuse it as
you wished.

So you could define and use your own type-checking classes and
semantics:

def bar(@StrictlyInt x, @DuckCompatibleSequence seq) :
...body...

Since the decorator is an *object*, not a function, we could add other
methods for use by IDEs for intellisense etc.

Any views on this? Something to put forward to the python-3000 list
maybe?

May 16 '06 #1
1 1130
Hi,

-1 because I find it extremly hard to read and not necessary in that
scale.

Actually, there are a lot of recipes in the Cookbook [1] on how to use
decorators for type-checking. On example is:

@require(int, int)
def add(x,y): return x + y

Which I find much more readable, easier to implement and even backwards
compatible:

def add(x,y): return x + y
add = require(int, int)(add)

g2g,
Justin

[1] http://aspn.activestate.com/ASPN/Cookbook/Python/

May 16 '06 #2

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...
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...
4
by: RebelGeekz | last post by:
Just my humble opinion: def bar(low,high): meta: accepts(int,int) returns(float) #more code Use a metadata section, no need to introduce new messy symbols, or mangling our beloved visual...
11
by: Arien Malec | last post by:
I've been following the decorator debate with some interest, and it's taken me a reasonably long time to understand what is meant by a decorator. One of the issues is that the Decorator pattern is...
13
by: km | last post by:
Hi all, was going thru the new features introduced into python2.4 version. i was stuck with 'decorators' - can someone explain me the need of such a thing called decorators ? tia KM
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: ...
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: Scott SA | last post by:
Hi, I've been trying to wrap my head around decorators and in my trails found avery recent article on Linux Mag's website. I didn't see a post here regarding this article and feel it is worth...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...
0
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...

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.