Next step after pychecker

S

Steve Holden

Skip said:
Francis> "Every well-formed expression of the language can be assigned a
Francis> type that can be deduced from the constituents of the
Francis> expression alone." Bird and Wadler, Introduction to Functional
Francis> Programming, 1988

Francis> This is certainly not the case for Python since one and the
Francis> same variable can have different types depending upon the
Francis> execution context. Example :

Francis> 1- if a is None:
Francis> 2- b = 1
Francis> 3- else:
Francis> 4- b = "Phew"
Francis> 5- b = b + 1

Francis> One cannot statically determine the type of b by examining the
Francis> line 5- alone.

Do you have an example using a correct code fragment? It makes no sense to
infer types in code that would clearly raise runtime errors:

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: cannot concatenate 'str' and 'int' objects

Also, note that the type assigned to an expression may be nothing more than
"object". Clearly that wouldn't be very helpful when trying to write an
optimizing compiler, but it is a valid type.

Skip

So reaplce the plus sign with an asterisk ...

regards
Steve
 
S

System Administrator

Francis> But Skip, I am sure that you can easily find an example by
Francis> yourself. For example, replace "+" by a function that does
Francis> different things depending on its argument type.

Sure. I was thinking of the optimization use of type inferencing,
forgetting the subject of the note.

Coming back to this:

1- if a is None:
2- b = 1
3- else:
4- b = "Phew"
5- b = b + 1

pychecker should be able to warn you today (but it doesn't) that you are
using b to refer to objects of two different types. It's not type
inferencing, but it would probably be a reasonable addition to its suite of
things it checks.

Skip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,816
Messages
2,569,706
Members
45,495
Latest member
cberns21

Latest Threads

Top