[ python-Feature Requests-778763 ] Optional type enforcement

SourceForge.net noreply at sourceforge.net
Fri May 21 12:25:01 EDT 2004


Feature Requests item #778763, was opened at 2003-07-28 06:18
Message generated for change (Comment added) made by arigo
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=778763&group_id=5470

Category: Python Interpreter Core
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Matt Perry (occupant4)
Assigned to: Nobody/Anonymous (nobody)
Summary: Optional type enforcement

Initial Comment:
One of the things that makes Python easy to code in is
the fact that you don't have to explicitly declare the
types of variables, or associate a specific type with a
variable.  However, this can potentially lead to
somewhat confusing and hard-to-maintain code, not to
mention make it easier to introduce bugs.

An idea struck me that it might be possible to combine
the power and safety of a strictly type-checked
language with the speed and ease of coding of Python. 
Normal variables as they stand now would be unaffected
by this feature, and retain their polymorphic type
capabilities.  Allow the programmer to optionally
declare the type of a variable somehow, be it with the
C-like syntax "int x", or Pascal's (I think?) "x: int",
or a new syntactic representation.  Python could then
issue a TypeError if the program attempts to assign a
non-int to x.  Obviously this feature could apply to
any data type, such as string, list, or perhaps
user-defined classes.

Class member variables can be declared as now in the
class definition body, with the same syntax to specify
a type, ie:
   class Test:
       int x = 5
       string name = "test"
       untyped = None

In addition, adding a type specifier to a function's
parameters could serve another means of distinguishing
it from other functions, which could allow function
overloading, ie:
   def double(int x):
       return 2*x
   def double(string x):
       return x + ' ' + x

Advantages:
- clarifies the author's intention in writing code, and
could help prevent confusion as to what type a
variable/parameter is.
- helps prevent bugs due to type mistakes
- doesn't interfere with existing code or coding style,
as dynamically typed variables are still allowed
- could allow function overloading if it was wanted

Disadvantages:
- implementing a type checker could be difficult?
- potentially pollutes namespace with extra keywords
like 'string' or 'list'.  may need to choose different
keywords.

----------------------------------------------------------------------

>Comment By: Armin Rigo (arigo)
Date: 2004-05-21 16:25

Message:
Logged In: YES 
user_id=4771

This idea has always been considered but there are very deep
issues with it. You should give a look to Pyrex, whose
language a more static and type-checked version of Python.
It produces C extension modules for the regular Python
interpreter, so it can be mixed with regular Python code
easily.  google:python+pyrex


----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2003-08-05 11:52

Message:
Logged In: YES 
user_id=80475

To make the most of polymorphism, interface checking would 
be preferable to enforcing specific types.  For example, it is 
better to check that f is a filelike type rather than requiring 
that is actually be a file -- that way, StringIO objects can be 
substituted without breaking code.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=778763&group_id=5470



More information about the Python-bugs-list mailing list