From Java to Python: beating the Stockholm syndrome
-
Upload
javier-arias-losada -
Category
Technology
-
view
932 -
download
0
description
Transcript of From Java to Python: beating the Stockholm syndrome
![Page 1: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/1.jpg)
from java to python
beating the stockholm syndrome
Javier Arias Losada@javier_arilos
![Page 2: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/2.jpg)
We are going to compare Java versus Python through some random topics… but not trying to be exhaustive or follow any logical patterns… just aspects I liked, disliked or just caught my attention.
Disclaimer1. Some points have been improved in latest versions of Python and Java
2. Opinions stated are those of the author, Javier Arias
![Page 3: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/3.jpg)
Working since 1997 with OO languages, mainly in Java and some Smalltalk… one has some rigidness regarding idioms, mental schemes and patterns…
Python approach to many aspects is different from Java and tends to be more pragmatic.
![Page 4: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/4.jpg)
verbosity and ease of reading (I)
http://stackoverflow.com/questions/5868369/how-to-read-a-large-text-file-line-by-line-in-javahttp://stackoverflow.com/questions/8009882/how-to-read-large-file-line-by-line-in-python
with open(...) as f:
for line in f:
# do something with line
“(…) when you program, you have to think about how someone will read your code, not just how a computer will interpret it.”- Kent Beck
Indentation, expressiveness and idioms, libraries matter.
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
// do something with line.
}
br.close();
![Page 5: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/5.jpg)
verbosity and ease of reading (II)
logical expressions & truthy values:
if x and x not in elements: print ‘done’
if (x != null && !elements.contains(x)){ System.out.println(“done”);}
Python seems to be ahead here, improves this productivity?
Usually code is easier to read and maintain in Python.
Refactoring is faster and more secure in Java (static typing + advanced IDE’s).
![Page 6: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/6.jpg)
verbosity and ease of reading (III)
Python keyword args: named and optional, default values.
def search(txt, file=’./f.txt’, lines=0): return 0
public int search(String txt, String file, Integer lines){ return 0;}
method and function definitionAn example: parse N lines of a file, searching for a text
Java method overriding: different methods with different parameters (number and type)
![Page 7: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/7.jpg)
verbosity and ease of reading (IV)
Lets add optional params in Java:
public int search(String txt, Integer lines){ search(txt, “./f.txt”, lines);}
public int search(String txt, String file){ search(txt, file, 0);}
Java’s method signature overriding is very useful for maintaining functions (and constructors) less cluttered.
![Page 8: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/8.jpg)
verbosity and ease of reading (V)
New Requirement: extend our search function to search for a client’s name, accept instances of Client class.
Results of the exercise:Java: one core method and three “facilitator” methods.Python: one method that is caring about type of parameters.
public int search(Client client, String file){ search(client.getName(), file);}
def search(what, file=’./f.txt’, lines=0): if isinstance(what, Client): what = what.name (...)
![Page 9: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/9.jpg)
verbosity and ease of reading (VI)
List and dictionary comprehensionsa real example: filtering a dictionary.
filteredPolicies = sprMessage.getPolicies().keySet()
.retainAll(GCM_ALLOWED_POLICIES);
filtered_policies = {pol: spr_message.policies[pol] for pol in _GCM_ALLOWED_POLICIES if pol in spr_message.policies}
Python Dict and list comprehension are very flexible and powerful ways of creating lists and dict on the fly.
Java is usually more verbose and less fluent for this collection related tasks, but sometimes a well designed API can do miracles, as in this example.
![Page 10: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/10.jpg)
verbosity and ease of reading (end)
As an objective indicator, searched google looking for the shortest tutorials of each language:
Teach yourself Java in 21 minutes:
http://www.debevc.uni-mb.si/IT2003/Razno/teach_Java_21_minutes.pdf
Learn Python in 10 minutes:
http://www.stavros.io/tutorials/python/
Python is half as verbose as Java.
If you ignore me, in the time of this talk you can learn both languages.
Just joking :-)
![Page 11: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/11.jpg)
as object oriented languages (I)inheritance
Java
simple inheritance.
interfaces.
statically-strongly typed.
Python
multiple inheritance.
dynamically-strongly typed.
Liskov Substitution Principle: If S is a subtype of T, then objects of type T may be replaced with objects of type S.
Use your code without caring about what type or subtype it is.
![Page 12: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/12.jpg)
Java super
super();
super.aMethod();
super always call parent.
Python super
super(class, object)
super may not call parent: MRO A
/ \
B C
\ /
D
as object oriented languages (II)composition-inheritance-multiple inheritance
“In object-oriented programming, Inheritance is the evil forest (...) deep inside the Dark Forest Inheritance is the Evil Queen Multiple Inheritance (...)” - Learn Python the hard way: http://bit.ly/O709e9
About MRO in Python: http://bit.ly/18RHQBC
![Page 13: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/13.jpg)
as object oriented languages (III)inheritance
The Circle / Ellipse problem:➔ In maths, circle is a special case for an ellipse: both axes
are equal
➔ Who should be superclass and subclass?◆ If Ellipse is the parent: what happens with the Ellipse method that
stretches an axis? what does that mean for Circle instances?
◆ If Circle is the parent, seems counterintuitive and no code reused.
Two ways of thinking about subclassing:
1. The OO classic view2. Pragmatic’s non-taxonomy based approach
![Page 14: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/14.jpg)
as object oriented languages (IV)inheritance, classic OO view
a subclass is a specialization of a superclass.
Organize your code by taxonomies: "Is-a" relationships. e.g. Dog is-an Animal.
"Inheritance is the idea that one class is a specialization of another class." - Code Complete.
This is what the majority of us do: at least in Java, probably in Python as well.
![Page 15: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/15.jpg)
as object oriented languages (V)inheritance, alternative approach
From The art of subclassing, Raymond Hettinger."How do you decide which one is on top? There is no principle of specialization. (...) Why do we subclass? Is just a way to reuse code. (...) the one that has more code that is reusable by the other class should be the parent. If the Dog has more code that is useful for Animal, put the Dog on top.” http://www.youtube.com/watch?feature=player_detailpage&v=miGolgp9xq8#t=640
Inheritance as a mechanism for code reuse,
nothing to do with taxonomies.
Raymond Hettinger, Python core developer:
Put the Dog on top,if the Dog has more code that is useful for Animal
![Page 16: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/16.jpg)
as object oriented languages (VI)polymorphism [1]
- ad-hoc polymorphism: function overloading [already covered]
- parametric polymorphism: generic type that can handle different types without worrying about their types.
Java: generics:
Python: builtin:
[1]three types of polymorphism, according to wikipedia http://en.wikipedia.org/wiki/Polymorphism_(computer_science)
l = []
List<String> l = new ArrayList<String>();
![Page 17: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/17.jpg)
as object oriented languages (VII)
public class Duck extends Bird implements Swimmer{
public String quack(){ (...)
public void walk(){ (...)
(...)
Duck donald = new Duck();
donald.walk();
donald.fly();
donald.quack();
donald.swim();
polymorphism- subtyping polymorphism: duck and chicken walk differently
Java: inheritance + interfaces + static typing
[1]three types of polymorphism, according to wikipedia http://en.wikipedia.org/wiki/Polymorphism_(computer_science)
![Page 18: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/18.jpg)
as object oriented languages (VII)polymorphism- subtyping polymorphism: duck and chicken walk differently
Python: inheritance + duck typing + protocols
class Duck(Bird):
def quack(): (...)
def walk(): (...)
(...)
donald = Duck()
donald.walk()
donald.fly()
donald.quack()
donald.swim()
[1]three types of polymorphism, according to wikipedia http://en.wikipedia.org/wiki/Polymorphism_(computer_science)
![Page 19: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/19.jpg)
as object oriented languages (VII)encapsulation:OCP (open closed principle) : "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification" http://en.wikipedia.org/wiki/Open/closed_principle
visibility (public, protected, ...), javabeans (Java)
versus
"_", "we are all consenting adults here" (Python) [1]
Python name mangling.
[1] from python mail: https://mail.python.org/pipermail/tutor/2003-October/025932.html
![Page 20: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/20.jpg)
execution model (I)Python execution model differs from those of Java and C++.
In Python everything is a name bounded to an object.
In Python, LoC are executed. No separation between declaration and execution.
Functions (def)
Classes (class)
Modules (import)
Entry points to code are the same modules
In Java class lifecycle is separated in load, initialization and execution.
1. Class resolved and loaded.2. Class is initialized.3. Code in class used.
Entry points to code well defined: main
![Page 21: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/21.jpg)
execution model (III)
● Diferenciate explicit execution from import time execution?
if __name__ == ‘_main__’:
“””your program here”””
![Page 22: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/22.jpg)
execution model (III)● implicit variable declaration● duplicate declarations● parameter default values that are mutable
class A(object):
def __init__(self, p1=0,
p2=None,p3=[]):
self.v1 = p1
self.v2 = p2
self.v3 = p3
def method1(self, p):
self.v3.append(p)
def method1(self, p1, p2):
self.lastp1 = p1
self.v3.append(p2)
print self.lastp1
In [5]: a = A(p2=3)
In [6]: a.method1(1, 2)
1
In [9]: print a.lastp1
1
In [10]: a2 = A(p1=3)
In [12]: print a2.v3
[2]
In [13]: print a.v3
[2]
![Page 23: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/23.jpg)
execution model (IV)monkey patching
Remember: in Python everything is a name bounded to an object.
In [22]: a = A()
In [23]: a.method1(1, 2)
1
In [24]: def m1(self, p, p2):
print 'modified ' + str
(p)
....:
In [25]: A.method1 = m1
In [26]: a.method1(1, 2)
modified 1
class A(object):
def __init__(self, p1=0,
p2=None,p3=[]):
self.v1 = p1
self.v2 = p2
self.v3 = p3
def method1(self, p):
self.v3.append(p)
def method1(self, p1, p2):
self.lastp1 = p1
self.v3.append(p2)
print self.lastp1
![Page 24: From Java to Python: beating the Stockholm syndrome](https://reader034.fdocuments.us/reader034/viewer/2022052505/554f5fc5b4c905bb178b4642/html5/thumbnails/24.jpg)
wrap up
You solve the problems.
The most important part of any language is you, the developer.
No clear winner, today I prefer Python.
Thanks for attending!