I've seen type checking decorators that do what I think you're describing - run-time type checking. Example 4 in PEP318 comes to mind:<br><br><div style="margin-left: 40px;"><a href="http://www.python.org/dev/peps/pep-0318/#examples">http://www.python.org/dev/peps/pep-0318/#examples</a><br>
</div><br>Probably won't be added to the language syntax, however, although I could see it sometimes being useful.<br><br>Cheers,<br>Andrey<br><br><div class="gmail_quote">On Sat, Sep 19, 2009 at 4:19 AM, Dj Gilcrease <span dir="ltr"><<a href="mailto:digitalxero@gmail.com">digitalxero@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">I do not expect this idea to gain much traction but it has been an<br>
idea rolling around in my head for a year or two, so I decided to<br>
write it all down and see what other thought.<br>
<br>
<br>
<br>
Abstract:<br>
Python does not currently support static typing of variables, which is<br>
something I like, but there are times it would be nice to have static<br>
typing for variables, or method/function return values.<br>
<br>
Static Ducks should be able to run along side or imported to code that<br>
does not use Static Ducks. Actual runtime enforcement of Static Ducks<br>
should be off by default and require activation either via the command line<br>
or at the top of a file (similar to the __future__ requirement)<br>
where it would<br>
affect only that file (and any Static Ducks imported to it)<br>
<br>
Design Goals:<br>
The new syntax should<br>
* Be simple to read<br>
* Make it obvious what is happening; at the very least it should be<br>
obvious that new users can safely ignore it when writing their own code<br>
* Allow future compilers to optimize for Static Typing.<br>
* Work within a function or method definition to ensure a passed<br>
variable is the proper type<br>
* Allow Functions/Methods return values to be typed and checked<br>
* Raise an TypeError exception when the variable is set to something<br>
improper, or the function/method tries to return an improper value<br>
<br>
Issues:<br>
If this PEP is implemented it may conflict with PEP 362<br>
(Function Signature Object)<br>
<br>
Use Cases:<br>
Python to C compilers could be enhanced by allowing static ducks.<br>
IDE's code complete could be enhanced by having static ducks available<br>
IDE's issues prediction could be enhanced to detect type issues for code<br>
that uses static ducks (Like any static typed language)<br>
<br>
Syntax Idea:<br>
"""$type variable = value<br>
<br>
$returnType def function($type variable):<br>
#do stuff<br>
return value<br>
<br>
$returnType<br>
def function($type variable):<br>
#do stuff<br>
return value<br>
<br>
class test:<br>
$type variable = value<br>
<br>
$returnType def method(self, $type variable):<br>
#do stuff<br>
return value"""<br>
<br>
The reason I selected $type is because I couldn't think of anything else<br>
that $ was used for and I wanted a syntax similar to Decorators and similar<br>
to other statically typed languages.<br>
<br>
All types allow None or their proper type when setting, this so people<br>
can have typed variables that can be "undefined" for checking<br>
<br>
Example:<br>
$int count = 0<br>
<br>
$bool def check($string testValue):<br>
return testValue.lower() == "true"<br>
<br>
<br>
class test:<br>
$string _name = None<br>
<br>
$string def get_name(self):<br>
return self._name<br>
<br>
def set_name(self, $string name):<br>
self._name = name<br>
<br>
Usage Details:<br>
When a variable has been statically type, any time it is set later its type<br>
needs to be verified. I see this working in a similar way to how<br>
properties work. Also using this style would allow people to create<br>
their own static ducks.<br>
<br>
"""psudo-code<br>
class StaticDuckBase(object):<br>
_value = None<br>
<br>
class IntTypeClass(StaticDuckBase):<br>
def _get_value(self):<br>
return self._value<br>
<br>
def _set_value(self, val):<br>
if isinstance(val, (int, type(None))):<br>
self._value = val<br>
else:<br>
try:<br>
val = int(val)<br>
self._value = val<br>
except:<br>
raise TypeError("Expected int, got " +<br>
str(type(val)) + " instead!")<br>
<br>
value = property(_get_value, _set_value, None)<br>
<br>
class MyTypeClass(StaticDuckBase):<br>
def _get_value(self):<br>
return self._value<br>
<br>
def _set_value(self, val):<br>
if isinstance(val, MyType)):<br>
self._value = val<br>
else:<br>
raise TypeError("Expected MyType, got " +<br>
str(type(val)) + " instead!")<br>
<br>
value = property(_get_value, _set_value, None)<br>
<br>
staticducks.add('mytype', MyTypeClass)<br>
"""<br>
<br>
Though there will need to be some way to check if a variable is<br>
statically typed so that when you do $int count = 0 then later count = 5 it<br>
uses the static typing. Also if no static duck is specified it should<br>
revert to nonstatic but emit a warning that is only show when run<br>
with the -W option<br>
<br>
Type List:<br>
Base Types:<br>
$int<br>
$float<br>
$bool<br>
$string<br>
$tuple<br>
$namedtuple<br>
$list<br>
$set<br>
$frozenset<br>
$dict<br>
$byte<br>
$bytearray<br>
$memoryview<br>
$nonstatic # This means treat it as if it were not statically typed<br>
<br>
Composite type<br>
$bytes -> $byte | $bytearray<br>
$number -> $int | $float | Decimal<br>
$seq -> $tuple | $list | $set | $frozenset | $namedtuple<br>
$sets -> $set | $frozenset<br>
<br>
Special Cases<br>
#lets you require a specific type for keys and values<br>
$dict($type key[, $type value])<br>
"""eg<br>
$dict($int, $string) tmp = {} # creates a dict that requires<br>
ints as keys and strings as values<br>
$dict($nonstatic, $bool) tmp = {} # creates a dict that<br>
requires values to be bool, but keys can be anything<br>
"""<br>
<br>
$tuple($type value) # lets you specify the type for the tuples values<br>
$list($type value) # lets you specify the type for the lists values<br>
$set($type value) # lets you specify the type for the sets values<br>
$frozenset($type value) # lets you specify the type for the sets values<br>
$sets($type value) # lets you specify the type for the sets values<br>
$seq($type value) # lets you specify the type for the sequence values<br>
<br>
$string(encoding="UTF-8", normalized=False)<br>
$normalized # This would be equiv of doing $string(normalized=True)<br>
$normalized(encoding="UTF-8")<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
</blockquote></div><br>