The Artima Developer Community
Sponsored Link

Weblogs Forum
Adding Optional Static Typing to Python

49 replies on 4 pages. Most recent reply: Jul 22, 2010 10:29 AM by Dmitry Dvoinikov

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 49 replies on 4 pages [ « | 1 2 3 4 ]
Jim Fulton

Posts: 3
Nickname: j1m
Registered: Jan, 2005

Zope Interfaces Posted: Jan 11, 2005 3:56 AM
Reply to this message Reply
Advertisement
For reference purposes, information on Zope interfaces
can be found at:
http://svn.zope.org/Zope3/trunk/src/zope/interface/README.txt?view=markup

Greg Janée

Posts: 1
Nickname: gjanee
Registered: Feb, 2005

Re: Adding Optional Static Typing to Python Posted: Feb 7, 2005 3:00 PM
Reply to this message Reply
Regarding complex type specifications, if you don't want to add new language syntaxes for describing types, you can go the way of Common Lisp and use various kinds of nested lists of symbols. For example, in Common Lisp, "(vector (or integer string) 100)" describes a vector of 100 integers or strings. I adapted this approach for Python using tuples: in my formulation, "(list, [int, str], 1)" describes a list of one or more integers or strings. More details at http://www.alexandria.ucsb.edu/~gjanee/archive/2005/python-type-checking.html . I'm not necessarily advocating this approach for the problem Guido is addressing, but the Common Lisp approach is an instructive data point if you're not familiar with it.

Ronald Adam

Posts: 5
Nickname: ron100
Registered: Mar, 2005

Re: Adding Optional Static Typing to Python Posted: Mar 14, 2005 5:02 PM
Reply to this message Reply
I'm proablaby a bit late in this, but maybe not.

How about adding an 'as' keyword? This would result in very readable prototype style definitions. I'm not sure how difficult this would be to do, but from a readability point of view, this seems to me to more consistant with pythons syntax.

def gcd(a, b) as int(int,int):
while a:
a, b = b%a, a
return b

And possibly usefull in the case of classes:

class abd as constant: # read only
pi = 3.14159265356
y = pi * 2 # this would be evaluated once

or single varibles:

a as int = 1
b as float = 3.5

The "as 'type'" statement would have the meaning of, "From now on, this name can only point to objects of this type."

So after which, if you want to assign a varable to something else you would need to remove the constraint or put on a new one.

a as int = 1
a as float = 5.4321
a = 3.14

a as none = 'what ever'
# "as none" removes a type constraint

The arguments to 'as' might also be put into quotes as a string to reduce the need to create a whole set of key words... int, float, long, string... etc.

def gcd(a, b) as 'int(int,int)':
while a:
a, b = b%a, a
return b

c as 'int' = 25
d as 'string' = 1.234
e as 'bool' = True

This form is very readable to me, something I've come to love and expect in python.

Ronald Adam
radam2_at_tampabay.rr.com
(replace _at_ to email)

Steve Donovan

Posts: 24
Nickname: stevedonov
Registered: May, 2005

Re: Adding Optional Static Typing to Python Posted: Jun 21, 2005 6:26 AM
Reply to this message Reply
The Boo language uses the 'as' keyword, but tends to lean heavily on type inference. It's usually not necessary to specify the return type of a function, ditto for assignments. But you do need to explicitly type function arguments, which is where they are terribly useful for maintenance purposes. And it's still as cool to use as Python ;)

steve d

Dmitry Dvoinikov

Posts: 253
Nickname: targeted
Registered: Mar, 2006

Re: Adding Optional Static Typing to Python Posted: Jul 22, 2010 10:29 AM
Reply to this message Reply
I've been using a special decorator (@typecheck) combined with annotations since 2008 for Python 3 and since 2006 for Python 2.

The idea is simple: function annotations serve for method signature specification. Not only argument types can be specified, but domains and/or restrictions in general. In my opinion static typing is not nearly as useful as ability to check/ensure/enforce certain restrictions on parameters at well defined points. This way, it is a form of assertion rather than type specification.

With this decorator you for example can write this:

@typecheck
def foo(a: int) -> str:
...

@typecheck
def str2int(s: by_regex("^[0-9]+$")) -> int:
...

@typecheck
def max(x: either(int, float), y: one_of(1, 2)):
...

@typecheck
def apply(f: callable, r: with_attr("next")):
...

@typecheck
def bar(a: list_of(str), b: optional(dict_of(int, anything)) = None) -> lambda x: x > 0:
...

Python 3 version is here:
http://code.activestate.com/recipes/572161/

Python 2 version is here (the syntax looks awkward in the absence of function annotations):
http://code.activestate.com/recipes/426123/

Flat View: This topic has 49 replies on 4 pages [ « | 1  2  3  4 ]
Topic: Have Generics Killed Java? Previous Topic   Next Topic Topic: Reviewable, Runnable Specifications Ensure Software Quality

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use