!geek

Code, learn & share

Common mistakes while switching from ruby to python

This is more of a note to self sort of post to talk about silly mistakes which can take you down quietly when you switch from ruby to python.

Problem 1 : The method which does nothing

person.rb (Ruby)
1
2
3
4
5
6
7
8
9
10
11
12
class Person
  def initialize(name)
      @name = name
  end
  
  def say_hello
      puts "#{@name} says hello"
  end
end

bob = Person.new('Bob')
bob.say_hello # Bob says hello
person.py (Python)
1
2
3
4
5
6
7
8
9
10
11
12
13
class Person(object):
  def __init__(self, name):
      self.name = name
  
  def say_hello(self):
      print("%s says hello" % self.name)


bob = Person('Bob')
bob.say_hello # Danger! Danger! Method won't be called

# After breaking your head for a while. Correct it to
bob.say_hello() # Well remember the good old parenthesis to call function?

Problem 2 : The method which returns wrong value

calculator.rb (Ruby)
1
2
3
4
5
def add(x, y)
  x + y
end

add(2, 3) # returns 5
calculator.py (Python)
1
2
3
4
5
6
7
8
def add(x, y):
  x + y

add(2, 3) # Danger! Danger! Returns nil 

# After breaking your head for a while. Correct it to
def add(x, y):
  return x + y

Solution

Test Driven Development

Comments