Newbie: static typing?

C

Chris Angelico

I guess this is a long way of saying instrument your software so that it can
be tested and or give you enough information about the internal state. This
is sort of like building a specialized integrated circuit. You need to
design it so it can be tested/observed after it's been embedded in epoxy and
not just count on being able to probe the wafer in the lab.

In software, that's easy: just have a way to execute arbitrary code in
the context of the running server. A *hugely* beneficial debugging
tool. Of course, it's also a security concern, so you have to put a
good password [1] on it, or have some other system for guaranteeing
that untrusted persons can't execute arbitrary code.

ChrisA

[1] By which I mean http://xkcd.com/936/ compliant.
 
G

Grant Edwards

Is there any pythonic way to perform static typing?

No.

One of the fundamental characteristics of Python is dynamic typing.

Without dynamic typing, it wouldn't _be_ Python.
 
D

Dennis Lee Bieber

If the type problems aren't caught right away when the invalid types are
passed to a function then the problem may only manifest itself in some far
away point in the code, making this bug needlessly harder to spot and fix,
and making the whole ordeal needlessly too time consuming.
Then wrap your code with a try/except block, allowing you to report a
misfunction at your level. That way anything that is viable can still work
without you restricting users of your module, yet you can handle the
failure, remove the extraneous (to you) low-level error trace, and raise a
failure apropos to your module.
 
T

Terry Reedy

There are two problems that can result from not checking:

1) The traceback will be deeper and may be less clear.

2) Some code will be executed and then an exception thrown.

3) The code falls into an infinite loop or recursion.

The solution is to think before looping or recursing. This often
involves value checking (non-negative int or non-fractional float, for
instance) rather than type checking in the usual static type-checking sense.

One also needs to be careful about passing unbounded iterators to other
functions and remember that unboundedness is contagious. (filter(pred,
unbounded_iterator) is an unbounded iterator). Again, this is a 'value'
or implicit sub-type issue rather than a explicit, visible 'type' issue.

Infinite recursion will be caught eventually when memory runs out (and
that is an advantage of recursion over iteration ;-), but I prefer to
avoid it anyway.
 
C

Chris Angelico

3) The code falls into an infinite loop or recursion.

The solution is to think before looping or recursing. This often involves
value checking (non-negative int or non-fractional float, for instance)
rather than type checking in the usual static type-checking sense.

Yeah, there aren't many languages that let you declare that the
argument must be a positive integer. That's just something you have to
test for manually.
One also needs to be careful about passing unbounded iterators to other
functions and remember that unboundedness is contagious. (filter(pred,
unbounded_iterator) is an unbounded iterator). Again, this is a 'value' or
implicit sub-type issue rather than a explicit, visible 'type' issue.

Not quite always; I'd say that unboundedness is as contagious as IEEE
Infinity. Lots of operations on infinity will yield infinity, but a
few won't. itertools.islice can guarantee a finite iterator, and
takewhile may terminate. But yes, with filter() it certainly is.

ChrisA
 
P

Prasad, Ramit

Rui said:
If the type problems aren't caught right away when the invalid types are
passed to a function then the problem may only manifest itself in some far
away point in the code, making this bug needlessly harder to spot and fix,
and making the whole ordeal needlessly too time consuming.


Rui Maciel

This can be true, but in personal experience does not happen
often. I will say that dynamic typing ends up usually
being more future proof as I can later create a similar object
(but not in the same inheritance hierarchy) that will work
with older functions because the functions don't look for
certain types but rather just rely on duck typing.

I find this especially useful when testing or mocking. I can
create a test object and attach methods/attributes to the
test object to duck type as I desire to test the my desired code.

I think the following reads are very interesting
for people new to Python from other languages (not just Java).
http://dirtsimple.org/2004/12/python-is-not-java.html
(and the flip side) http://dirtsimple.org/2004/12/java-is-not-python-either.html


~Ramit



This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy andcompleteness of information, viruses, confidentiality, legal privilege, andlegal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.
 

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
474,123
Messages
2,570,741
Members
47,296
Latest member
EarnestSme

Latest Threads

Top