Photo by Bence Kondor from Pexels

Python vs JavaScript: Objects

Beniamin Hławiczka
2 min readFeb 22, 2021

--

Python object

In Python there are actually two kinds of objects. One — more primitive, is created by calling object() - it returns a new instance of built-in object class. And another - more common - the first one's extension - created by instancing a class. As doc says objects created directly from object() are featureless and they don't have a __dict__, so you can't assign arbitrary attributes to them. There are some use cases for such a simpler objects - for example if you need just a unique reference - like a sentinel. On the other hand, all user-defined classes by default have __dict__ (unless changed by the user), so they more resemble standard JS Object.

JS object

Likewise in Python, also in JavaScript we can distinguish two object types. But they are different in a different way. We can create a “standard” JS object, by using for example an object literal — {}. Or - we can also use Object.create(null) which creates a truly empty object whose prototype is null. This "emptier" version of an object works similar to its full version, except the fact that it doesn't have native Object prototype's methods like: .toString(), .valueOf(), .hasOwnProperty() etc. But unlike Python featureless object(), it allows for arbitrary attribute assignments, just like a standard JS object.

The comparison

Below there is a simple comparison between Object’s implementation in both languages. To avoid too broad scope object oriented concepts like classes or inheritance are not a subject of this comparison. Properties and attributes are used interchangeable meaning values set on object for given keys. Python 3 and a “modern” JS engine are assumed (V8, SpiderMonkey, JavaScriptCore).

Time complexity of common operations

Common object recipes

--

--