Polymorphism and Generics

  1. Polymorphism refers to the fact that method invocation of a variable (identifier) is applied by calling the specific method of the object that it refers to rather than the method defined by the variable's type. Polymorphism occurs when a parent method is overloaded by the child class. Or by method overload in the same class level.

  2. Generics refers to the mechanism that the type of the argument(s) allowed in class instantiation/method invocation is more than one.

  3. The possible overlapping of polymorphism and generics: method with only one parameter.

Python Java and C++,Part One

Entry Point

Java and C++ has the main method as the sole entry point, python has no official definition or requirement for entry point. In python, it is common to use a script called main.py as the entry point of the program (the interpreter starts running the main.py script first).

Arithmetic Evaluation (Division)

Python3 will by default evaluate integral division in a float division manner ( 1/2 = 0.5). Java, C++ and Scala will evaluate integral division in an integral division manner ( 1/2 = 0 )

Import System

  1. Python's real equivalence of java package is called module (which is actually a .py file). Python's package is equivalent to Java's package of package (nested package). Unlike a Python package, a Java package (a namespace) needs to be explicitly declared. Python allows programming at the package level. In Java, package is just a namespace.

  2. By default, in Java the import statement applies to class names in the package name space. Import is followed by the full name of a class (package.package.....class). However, the import statement in python applies to not only class names (from package.module import class), but also other variables defined in the module (functions, constants, etc). In addition, the import mechanism of python allows the import of only package name (while in case the module names can not be referenced via the package name if no corresponding imports are found in the "init.py" file of the package).

  3. After a full name import of module in Python, the imported module has to be referenced by the full name only. In Java, only the class name should be used for reference after the imports. To alleviate this problem, Python supports the use of alias by "as" keyword in import statement.

  4. Python supports defining variables and introducing names at the package level, which makes package level API design available.
    Example: import tensorflow as tf (all APIs are introduced into the tensorflow namespace by using imports in "init.py" file of package tensorflow)

In contrast, a java version of tensorflow API would have to be imported by the following statements: import tensorflow.api.xxx.
(In java, package object can not have methods/functions. Instead, all methods are defined in classes and must be introduced through importing classes)

Python's import mechanism is pretty messed up with lots of inconsistency, partly due to its natural as an interpreting language.

  1. Wildcard imports in python can only be used when "all" magic variable is defined in the "init.py" file of the package. Only names defined by "all" will be imported by the wildcard import statement.In Java, no such definition of wildcard names is required.

  2. Avoid using module attribute: "file" in any cases ( due to inconsistency and ambiguity), all module/package path issue should be resolved through entry point: sys.path[0] or cwd() (1 in 2).
    Note: After py3.4.3, "file" will return the absolute path of the module object when not invoked from "main". Using "file" would result in different behavior between running a script directly under cwd (using relative path) and running a script indirectly from cwd/abs path. Due to the fact that "file" is a module attribute, this different behavior can not be mitigated by patch wrapping.

  3. Good python API design should collect all important APIs in the package namespace through programming "init.py" file. This avoids explicit imports of all sub-modules and class/function names

Class Instantiation

Class instantiation in python does not require the use of new keyword.

Local Scope

All if and loop "block" in python has global scope.

Identity Test

  1. In python, "is" operator is the identity test, "a" is "b" is the equivalent of id(a) == id(b). "==" operator is the value equality test. When in the equality test, if the right operand is a primitive type object (an expression), then the "==" operator is safe to be replaced by "is" operator.

  2. java中的数据类型,可分为两类:
    当他们用(==)进行比较的时候,比较的是他们在内存中的存放地址,所以,除非是同一个new出来的对象,他们的比较后的结果为true,否则比较后结果为false。 JAVA当中所有的类都是继承于Object这个基类的,在Object中的基类中定义了一个equals的方法,这个方法的初始行为是比较对象的内存地 址,但在一些类库当中这个方法被覆盖掉了,如String,Integer,Date在这些类当中equals有其自身的实现,而不再是比较类在堆内存中的存放地址了。

pass by value v.s. pass by reference v.s. pass by object reference

Python and Java and C++ has its own language describes their object model and argument pass mechanism.

Note: Object reference means an object is bound to an identifier (called a variable), all changes to the variable applies to the object referenced as well. (Java: Pass the object reference by value).
A reference to a variable is treated exactly the same as the variable itself, any changes made to the reference are passed through to the argument.

Python is designed to be "pass by object reference", that is for primitive types and non-primitive types argument passing is done through passing of object reference (which is the id of this object, iid, memory address).

Java is designed to be "pass by value", for primitive types it is equal to value copying, for non-primitive types it is the copying and passing of the object's reference by value.

C++ has pass by value and pass by reference mechanism. Pass by value applies to both arithmetic values and object pointers (is itself an object) and class instances, it is essentially a copying of the argument (applies to class instance as well). Pass by reference applies to all types in that all changes to the parameters affects the arguments as well.

Note: The effect of C++ pass by reference cannot be produced in Java and Python (for both the primitive types and the non-primitive types), which is, a function can change the reference of the variable that acts as the argument of this function, iid, change the reference of outer scope variable via operation in the inner scope. (Needs verification).

The above mechanism reflects the justification of "pass reference by copying value".


In C++, a variable is a storage location. "int a = 1", assign value 1 to the address location of a.
In Python, a variable is a purely an identifier (bounded to certain objects). "a = 1", assign/change the object reference (not a reference in c++ context) of a to object 1.

Program Constructions

Java requires that all functions been implemented as methods of a class. Variables take two forms, one is the class members (attributes in JS, property member in python), the other is temp variables that reside in the local scope of methods.

C++ can have variables and functions defined/used in the global scope.
Python technically can only have variables and functions defined/used in the module and all kinds of local scope. "Global Scope" in python usually refers the module scope of the "main" module.

Pointer Declaration

In C++, the pointer is declared by adding a affix to a non-pointer type to construct a pointer type. int* a = &b
In golang, the pointer is declared by prefixing the non-pointer type to construct a pointer type. var a *int = &b