Python to Kotlin: Variables and Types


Python Variables and Types

As explained in the background, all values in python are objects stored in dynamic ‘heap’ memory, and variables are simply references to these objects. This means the only static or stack frame based information for any variable is the pointer to the object containing the actual value. Since the variable itself always holds the pointer, all variables will require the same amount of memory regardless of the value they pointing to. This means no type is needed to declare a variable, as every variable will require the same space allocation. As of python 3.6, you could write:

a: int
b: int = 6

a1: int = None # type hint but not strictly correct

from typing import Union
a2: Union[int, None] = None # now strictly correct

def myfun(arg1: int, arg2: str) -> int:

# the normal way python is written
a = None # or in reality, a not assigned until it is needed
b = 6

def myfun(arg1, arg2):

But all the types in the above code actually do nothing in python itself. A post processor such as ‘mypy’ is needed to give you warnings about types. The first statement “a: int” actually does nothing at all, and if the second was “b: int = ‘abc'” it would still work. Almost no python at this time is written with all these types, but Python now allows adding them because it is felt having the option to add types could, in some applications, make for more readable programs. However these annotations are just that, annotation.   The reality is Python variables do not have a type, it is the data the variables reference that has the type. You can check the type of any variable at any time, but the type being checked is the type of the object being referenced at that time, not the type of the variable itself.

Because any variable can reference any type, python is said to be dynamically typed.

Kotlin Variables and Types.

In koltin, every variable has a stated type. Again every variable, at least from a programmer point of view, is a reference to an object, so it would be possible to have the same variable reference any object type, and dynamically change what type is referenced.  However the language rules, for reasons to be covered elsewhere, enforce static typing and every variable has a predetermined and fixed type.  The programmer normally does not have to specify the type as it can usually be inferred.  The above code becomes:

var a: Int? = null  # cannot create property without initialisation
var b: Int = 6

// type can most often be inferred from initial value
var b0 = 6 // so b0 will also be of type Int 

fun myfun(arg1: Int, arg2: String) : Int{

This is almost the same as the python example with types, except that as we said, very little python code is actually written with all types annotated. Note that in kotlin, all classes, even the built in classes have the consistent leading capital. Python only breaks the ‘Capital’ for classes rule for the simple types because, in Python, ‘int’ and ‘str’ and other basic types were not actually classes when the language started.

Variable names are normally camelCaseNames instead of names_with_underscores as direct for Python by PEP 8. CamelCase does make the names slightly shorter, but is a matter of preference.

Kotlin is designed around this certainly of type, and has optimisation and toolchains for writing code that all build on the certainty of type at all times.

From a code readability perspective, it is clear that even some python programmers would like code to know the type, and many regard the type being clear a very good thing. There are many situations dealing with types that are new for python programmers and this will be covered in future pages.

Boxed Objects vs Unboxed Primitive types

Primitive types, as in the languages C or Java, are where an area of static memory holds the required value and are described in Variables and Static Memory

The name ‘boxed types’ is used to describe values that a language is by default considers a primitive type which could be held directly in static memory, that are instead instanced or ‘wrapped’ within an object, and objects are held in dynamic memory.  Here is an article on ‘boxing and unboxing in Java.

While it is typical for languages described as Object Oriented such a Java to also support explicit non-object primitive variables, neither Python nor Kotlin has any syntax for these non-object variables.  In both languages, from a programmer perspective, everything is an object.  However, underneath, the two languages are different.  Primitive, static memory based ‘unboxed’ variables are considerably more efficient in terms of execution time than boxed objects, and the static typing of Kotlin together with being a compiled language, allows the compiler to optimise the compiled code so the result can be unboxed fields and data for situations when the program makes no use of the data being an Object. This optimisation will naturally happen inside tight loops, and in other cases when execution is most critical, as it is in these cases where no use of the nature of an object occurs.   This is one of the paybacks of strict static typing, faster execution.


2 thoughts on “Python to Kotlin: Variables and Types”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s