Newbie: static typing?

Prasad, Ramit ramit.prasad at jpmorgan.com.dmarc.invalid
Thu Aug 8 12:46:03 EDT 2013


Rui Maciel wrote:
> Chris Angelico wrote:
> 
> > On Tue, Aug 6, 2013 at 10:01 AM, Rui Maciel <rui.maciel at gmail.com> wrote:
> >> It would be nice if some functions threw an error if they were passed a
> >> type
> >> they don't support or weren't designed to handle.  That would avoid
> >> having to deal with some bugs which otherwise would never happen.
> >>
> >> To avoid this sort of error, I've been testing arguments passed to some
> >> functions based on their type, and raising TypeError when necessariy, but
> >> surely there must be a better, more pythonic way to handle this issue.
> >
> > def add_three_values(x,y,z):
> >     return x+y+z
> >
> > Do you want to test these values for compatibility? Remember, you
> > could take a mixture of types, as most of the numeric types can safely
> > be added. You can also add strings, or lists, but you can't mix them.
> > And look! It already raises TypeError if it's given something
> > unsuitable:
> 
> 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 and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.  



More information about the Python-list mailing list