473,386 Members | 1,606 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,386 software developers and data experts.

type-checking support in Python?

I'm just re-learning Python after nearly a decade away. I've learned
a good healthy paranoia about my code in that time, and so one thing
I'd like to add to my Python habits is a way to both (a) make intended
types clear to the human reader of the code, in a uniform manner; and
(b) catch any type errors as early and automatically as possible.

I found the "typecheck" module (http://oakwinter.com/code/typecheck/),
but I notice that it hasn't been updated since 2006, and it's not
included with the Python distribution. Are there other modules
providing similar functionality that I should consider instead?

Also, I'll probably be considering a lint-like tool at some point...
are there any that allow you to declare some extra type information,
and have that checked at lint time?

Finally, one thing I've considered is adopting some identifier
prefixes indicating type. Maybe "iFoo" for integer, "fFoo" for
floating-point numbers, "d" for dictionary, "l" for list, "t" for
tuple, "o" for object, and "v" for variable types that may be more
than one of the above. I gather (from just a couple days of browsing)
that such a naming convention isn't common in the Python community,
but is there anyone else here who does it? I'd rather adopt an
existing standard (even if it's not widely used) than make one up.

Many thanks,
- Joe
Oct 6 '08 #1
4 1410
Joe Strout wrote:
I'm just re-learning Python after nearly a decade away. I've learned
a good healthy paranoia about my code in that time, and so one thing
I'd like to add to my Python habits is a way to both (a) make intended
types clear to the human reader of the code, in a uniform manner; and
(b) catch any type errors as early and automatically as possible.

I found the "typecheck" module (http://oakwinter.com/code/typecheck/),
but I notice that it hasn't been updated since 2006, and it's not
included with the Python distribution. Are there other modules
providing similar functionality that I should consider instead?

Also, I'll probably be considering a lint-like tool at some point...
are there any that allow you to declare some extra type information,
and have that checked at lint time?

Finally, one thing I've considered is adopting some identifier
prefixes indicating type. Maybe "iFoo" for integer, "fFoo" for
floating-point numbers, "d" for dictionary, "l" for list, "t" for
tuple, "o" for object, and "v" for variable types that may be more
than one of the above. I gather (from just a couple days of browsing)
that such a naming convention isn't common in the Python community,
but is there anyone else here who does it? I'd rather adopt an
existing standard (even if it's not widely used) than make one up.
The short answer is:

- don't use typechecks, or at least as few as possible - only for
polymorphy. Ducktyping is important for good python code, and shouldn't be
prevented by excessive typechecks.

- if you don't trust your code, write tests. Actually, *always* write
tests. It really helps getting the design of your code better, saves time
when refactorings or enhancements are needed - and captures more errors
that static typechecking - and your typechecking actually isn't even static
& thus you'd need tests to get code coverage anyway.

- don't adopt these silly microsoft perversion of hungarian notation.
clear, expressive variable names - sure. with pre- or suffixes to indicate
their kind, possible. But using iX or fX where all one cares is that X
holds a number is not helpful.

Diez
Oct 6 '08 #2
Joe Strout a écrit :
I'm just re-learning Python after nearly a decade away. I've learned a
good healthy paranoia about my code in that time, and so one thing I'd
like to add to my Python habits is a way to both (a) make intended types
clear to the human reader of the code,
Good naming and documentation.
in a uniform manner; and (b)
catch any type errors as early and automatically as possible.
For which definition of "type error" ?-)

To make a long story short, duck typing is the pythonic way to do
things. From experience, type errors are rare, and usually very quickly
spotted and fixed. Except when it comes to handling user inputs, don't
bother coding in a defensive style. Just write the minimal necessary
code, and only worry about exceptions you somehow except *and* can
handle locally.
I found the "typecheck" module (http://oakwinter.com/code/typecheck/),
but I notice that it hasn't been updated since 2006, and it's not
included with the Python distribution. Are there other modules
providing similar functionality that I should consider instead?
yes : unittests.

Also, I'll probably be considering a lint-like tool at some point... are
there any that allow you to declare some extra type information, and
have that checked at lint time?
Not AFAIK. This may come with py3k using ABCs and type annotations, but
once again, don't worry too much about this.

I know these advices may sound scary to declarative static typing
addicts, but man/years of experience have proven that duck typing
JustWork(tm). So don't fight the language, it would only make you suffer
useless pain and frustration.
Finally, one thing I've considered is adopting some identifier prefixes
indicating type.
Don't.
Maybe "iFoo" for integer, "fFoo" for floating-point
numbers, "d" for dictionary, "l" for list, "t" for tuple, "o" for
object,
Then you can already prefix each and any of your identifiers (including
classes and functions) with an 'o', since everything in Python is an
object !-)
I gather (from just a couple days of browsing) that such a
naming convention isn't common in the Python community,
It's not only uncommon, but - just like useless typechecking -
considered bad form.
but is there
anyone else here who does it? I'd rather adopt an existing standard
(even if it's not widely used) than make one up.
The standard coding conventions are here:
http://www.python.org/dev/peps/pep-0008/

FWIW, Python relies quite heavily on naming conventions. Better to stick
to pep8.
Oct 6 '08 #3
On 6 Okt., 16:19, Joe Strout <j...@strout.netwrote:
I'm just re-learning Python after nearly a decade away. *I've learned *
a good healthy paranoia about my code in that time, and so one thing *
I'd like to add to my Python habits is a way to both (a) make intended *
types clear to the human reader of the code, in a uniform manner; and *
(b) catch any type errors as early and automatically as possible.

I found the "typecheck" module (http://oakwinter.com/code/typecheck/), *
but I notice that it hasn't been updated since 2006, and it's not *
included with the Python distribution. *Are there other modules *
providing similar functionality that I should consider instead?
Incidentally I started to use the typecheck package just yesterday.
It's not that I'm using it in a typical application but I'm working on
a mapping of a statically typed language onto a Python framework that
emerges in parallel. So I have to rebuild the type semantics of the
original language in Python but defer typechecks until runtime.

My impressions so far have been mixed. I stumbled across some strange
failures that required uncommenting source code in the typecheck
package which might cause failures elsewhere. I also hit a barrier
when working with methods instead of functions. I also noticed that
passing two strings to a funtcion:

@accepts(Number, Number)
def add(x,y):
return x+y

is acceptable behaviour.

From all those experiences I'd rate the package alpha and I'm sad
noticing that it is apparently abandonware. I'll continue to use it
nevertheless.
Oct 13 '08 #4
In message
<70**********************************@v72g2000hsv. googlegroups.com>, Kay
Schluehr wrote:
... and I'm sad noticing that it is apparently abandonware.
In Open Source, nothing is ever truly abandonware unless nobody cares about
it any more. :)
Oct 14 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: bartek d | last post by:
Hello, Regarding my previous question about a class which is used to store a variable type vector. I tried to be more elaborate on the code. I'd be grateful for your suggestions. Am I going in...
0
by: Morten Gulbrandsen | last post by:
Dear MySQL developers, Could some experienced Database developer please take a look at this ? It is supposed to be plain SQL2. How can it be coded under MySQL Especially all referential...
4
by: Jari Kujansuu | last post by:
I can successfully parse XML document using SAX or DOM and I can also validate XML document against schema. Problem is that my program should deal with user-defined schemas which means that when...
0
by: chobin | last post by:
Hi all. I've a terrible question. I've to build one XML page (based on a xml schema) that implements a dynamic database. In particular, I would declare something like this: <?xml version="1.0"...
4
by: Richard Lee | last post by:
Hi, I have a question when I do a data type cast. the common way when we do a cast, is we know the type we want to cast to, i.e. we want to cast object to string, object xyz = "question";...
3
by: dgaucher | last post by:
Hi, I want to consume a Web Service that returns a choice, but my C++ client always receives the same returned type. On the other hand, when I am using a Java client, it is working fine (of...
11
by: Johan | last post by:
Hi Can somebody explain to me why I get this warning message and how I can solve this warning message. Thanks a lot Johan In member function `void
3
by: H. S. | last post by:
Hi, I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going...
5
by: Fabio Fracassi | last post by:
Hi, I belive what i am trying to do is not possible, but I hope someone can change my mind. Here is some code i'd like to write: template <class type> class engine1 {}; template <class...
5
by: Axter | last post by:
I'm fine tuning a scope_handle class that takes a policy class as the second template. http://code.axter.com/scope_handle.h Please see above link for full understanding of the problem. One...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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
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...

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.