Top gotchas for someone moving from static lang (java / C #) to a dynamic language like python

What are the top gotchas for someone moving from static lang (java / C #) to a dynamic language like python?

It seems healthy how everything can be done, but renaming a method or adding / removing parameters seems so risky!

Is the only solution for writing tests for each method?

+6
java python dynamic-languages
source share
3 answers

I would say that the resulting number one tries to write statically typed code in a dynamic language.

Feel free to use an identifier to point to a string and then a list in standalone sections of code

keys = 'foo bar foobar' # Imagine this coming in as an argument keys = keys.split() # Now the semantically chose name for the argument can be # reused As the semantically chosen name for a local variable 

Feel free to consider functions like ordinary values: they are. Take the following parser. Suppose we want to process all header tags, as well as ul tags, such as ol tags.

 class Parser(HTMLParser): def __init__(self, html): self.feed(html) def handle_starttag(self, tag, attrs): parse_method = 'parse_' + tag if hasattr(self, parse_method): getattr(self, parse_method)(attrs) def parse_list(self, attrs): # generic code def parse_header(self, attrs): # more generic code parse_h1 = parse_h2 = parse_h3 = parse_h4 = parse_h5 = parse_h6 = parse_header parse_ol = parse_ul = parse_list 

This can be done using less generic code in a handle_starttag method in a language such as java, tracking which tags map to the same method, but if you decide you want to process div tags, you must add this to the dispatch logic. Here you just add the parse_div method and you are good to go.

Do not use typecheck! Duck-type!

 def funtion(arg): if hasattr(arg, 'attr1') and hasattr(arg, 'attr2'): foo(arg): else: raise TypeError("arg must have 'attr1' and 'attr2'") 

unlike isinstance(arg, Foo) . This allows you to pass any object using attr1 and attr2 . This allows you, for example, to pass a trace class wrapped around an object for debugging purposes. You will need to change the class to do this in Java AFAIK.

As pointed out by THC4k, another (more pythonic) way to do this is through the EAPF idiom. I don’t like it because I like breaking errors as early as possible. This is more efficient if you expect code to rarely fail. Do not tell anyone, I do not like it, although we will stop thinking that I know how to write python. Here is an example courtesy of THC4k.

 try: foo(arg): except (AttributeError, TypeError): raise InvalidArgumentError(foo, arg) 

This is what we need if we need to catch AttributeError and TypeError or just let them spread somewhere who knows how to handle them, but this is just an example, so we will let it fly.

+2
source share

"The only solution for writing tests for each method?"

You say you did not write tests for each method in Java?

If you wrote tests for each method in Java, then nothing has changed, has it?

renaming a method seems so risky!

Correctly. Do not do this.

Adding / removing parameters seems so risky!

What? Are you talking about additional options? If so, then having multiple overloaded names in Java seems risky and confusing. Having additional options seems simpler.


If you are looking for SO for the most common Python questions, you will find that some things are chronic questions.

  • How to update PYTHONPATH .

  • Why some random floating point calculation does not coincide with mathematical abstraction.

  • Using Python 3 and entering code from a Python 2 tutorial.

  • Why Python doesn't have supercomplex protected , private and public declarations.

  • Why Python doesn't have an enum type.

Chronic issue # 1 seems to use mutable objects as default values ​​for a function. Just avoid it.

+3
source share

Some things that struck me when I first tried Python (based on the Java main background):

  • Write down the Pythonic code. Use the recommended idioms for Python instead of doing it in the old Java / C way. This is more than just a cosmetic or dogmatic problem. Pythonic code is actually much faster in practice than C-like code almost all the time. In fact, IMHO very much ignores the concept of "Python slow" because inexperienced coders tried to code Java / C in Python and ended up getting great success and got the idea that Python is terribly slow. Use lists and display / filter / reduce when possible.

  • Be satisfied that functions are indeed objects. Pass them as callbacks, return functions to functions, find out about closures, etc.

  • There are many cool and almost magical things in Python that you can use as a rename. These things are great to show Python functions, but are not really needed if you don't need them. Indeed, as S. Lott noted, it is better to avoid things that seem risky.

+2
source share

All Articles