Skip to content

Instantly share code, notes, and snippets.

@jtsaito
Last active April 10, 2016 09:12
Show Gist options
  • Save jtsaito/daca2a41f08e1ddab28952c94e821414 to your computer and use it in GitHub Desktop.
Save jtsaito/daca2a41f08e1ddab28952c94e821414 to your computer and use it in GitHub Desktop.
Trying out Python as a Ruby developer

Day 1

  • So there is pyenv. It seems to be the same as rbenv. Noone seems to be using it.
  • There is nothing like bundler. I'm going with pip install -r requirements.txt. This is like a mixture of a Gemfile and a Gemfile.lock but the actual libraries seem to be is stored globally. I am disappointed.
  • Testing. The standard choice here seems to be either pytest or the built in unit test. Neither of them seems to be BDD. Since Python does not have Ruby's blocks for writing DSLs everything looks really ugly compared to RSpec.

Day 2

  • Ruby: array.length vs Python len(list). Why do I have to call a globally defined function? Because the OO is done well?
  • In Ruby I can use inject with a block. Again, there are some functions in Python for filter, map and reduce. The reduce function was moved out of the standard library and into the functools library. Here is an example inject Ruby vs Python:
array.inject(0) { |result, element| result * element }
import functools
lam = lambda result, element: result * element
functools.reduce(lam, list)
  • The standard pylinter complains about naming a variable "list" because that is Redefining built-in 'list'. Having a varialbe called list results in trouble when debugging with pdb because you cannot call list.
  • The default linter settings require comments.
  • The linter enforces documentation. It seems Python emphasises documentation, Ruby readability of the code.
  • The linter prefers locally defined methods over lambdas by default
  • Class methdos: they come in two kinds @classmethod and @staticmethod. In any case, the syntax looks like a hack.
  • Python made a design choice to have all methods public. To compensate for this, there is a convention of using leading uderscores for methods which should not be called from outside the class. This results ugly methods everywhere.
  • "private" memeber methods have to be called using self. explicitely. Again, this is the opposite of the Ruby design.
  • Spacing: why 4?

Day 3

  • Nice: list slicing support in syntax. Maybe a bit too powerful (steps).
  • mixed blessing: list comprehensions and generators (lambdas still look more natural to me)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment