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)
123456789101112
classPersondefinitialize(name)@name=nameenddefsay_helloputs"#{@name} says hello"endendbob=Person.new('Bob')bob.say_hello# Bob says hello
person.py (Python)
12345678910111213
classPerson(object):def__init__(self,name):self.name=namedefsay_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 tobob.say_hello()# Well remember the good old parenthesis to call function?
Problem 2 : The method which returns wrong value
calculator.rb (Ruby)
12345
defadd(x,y)x+yendadd(2,3)# returns 5
calculator.py (Python)
12345678
defadd(x,y):x+yadd(2,3)# Danger! Danger! Returns nil # After breaking your head for a while. Correct it todefadd(x,y):returnx+y