Blog of Rob Galanakis (@robgalanakis)

Relearning python, day 3

The last few days were spent re-organizing my code and writing unit tests.  Here’s what I learned:

I was still spending too much time thinking about namespaces, privateness, interfaces, and organization.  Once I got rid of some ‘abstract’ classes that served no purpose (they’re not meant to be subclassed outside the library- so why bother?), stopped trying to hide things behind so many layers of indirection, and organized my code differently (didn’t have all my modules start with an underscore because I was so concerned about what I’d expose), something magical happened- the API was simplified.  It’s funny- creating simple APIs in C# often involves a ton of substructure that is all internal/protected/private- in fact, I think I excelled at making great public APIs, but they always involved a lot of private implementation (and that’s not just me- look at something like Enum.TryParse).  Creating a simple public API in python has the opposite effect- it seems to streamline the code and make it more explicit (as you’d expect).

Unittesting is awesome.  I still have a ways to go to learn how to write tests well, but that’ll come in time.  Unit testing gives me the benefits of static typing (ensuring what needs to be called is callable), and more (it tests actual functionality).  Doctesting is wonderful as well- I tend to test library/utility functions with doctest and unittest for everything more complex or things that have side effects.  I enjoy documenting in python far more than C#- I’m not sure exactly why, but having a much simpler usage and the ability to test/demo via documentation is great.  I enjoy the more flexible arbitrary string style of docstrings, rather than the heavier xml-style documentation in C#.  Almost everything I’ve written is tested, which is great (because it also means I am writing code that is actually testable and modular).

I can’t decide whether to use camelCasing or lower_with_underscore.  I prefer C#’s casing style, objectively (I liked it more even though I came from using camel-cased)- and MS is much more decisive on style, which means even if I didn’t agree, I’d adapt.

I want to start looking into multithreading and will miss .NET’s Task Parallel Library and ThreadPool.  I’ll start looking into stackless.

The biggest thing I realized is that python is a big paradox.  It is supposedly easy to learn and relatively simple; a great language for beginners.  But so much of good python programming is understanding convention, in that the language doesn’t force a certain way of doing things.  This makes python awful for beginners, and it has been my biggest criticism since I have been coding C#.  There is an awful amount of awful code written by people who have not gotten over the education hump.  C# and .NET force the programmer to do a large number of things.  They are good for teaching basics because you must follow these basics.  There is a lot more overhead to writing .NET than there is to python, and that overhead implies some more rigorous study.  There’s no convention- just static typing, and everything is an intellisense dropdown away.  Python requires more thought but less work, which means I feel incredibly liberated working in it.

 

One thought on “Relearning python, day 3

  1. Phil Horowitz says:

    Reading your thoughts on this has been awesome. I’ve wondered how Python functions beyond simple scripts since I picked c++ back up. I wish more people talked about the paradigms of c# and python like this, it really illuminates their strengths.

    I look forward to reading more as you discover python.

Leave a Reply