DEV Community

.
.

Posted on

Javascript vs Python: object

Python
Almost everything is an object (the if statement isn't, for example, but every float, string or a function is an object). "Variables" are names, and "variable assignment" (like a = 6) is actually binding of a name to an object. Each binding has a scope that defines its visibility.

A = []
B = A # both A and B are bound to the same object
B.append("hi") # object bound to B (and A) is mutated
print(A, B)
>>> ["hi"], ["hi"]
Enter fullscreen mode Exit fullscreen mode

So Python has mutable and immutable objects. For example, list is mutable, int is immutable.

Javascript
In JS, we also say that almost everything is an object. What isn't an object are primitive values - null, undefined, string, number, boolean and symbol. So apart from the obvious differences with Python (like the fact that Python doesn't have undefined and null only None and that is an object too), I want to talk a bit about what is meant by object in either language.

Object in JS is a "storage" of key:value pairs. Coming from python, when I first started using JS, I thought "aha, objects in JS are like dictionaries in python."

const myObj = {id: 5};
Enter fullscreen mode Exit fullscreen mode

But better equivalent of a Javascript object in Python is not a dictionary, it's a class. So when we say an object in python, we mean an instance of some class. But, both languages are object-oriented. It's just that in JS, there is no distinction between a class and an instance of a class.

Python: Class definition only says what methods and properties will every instance of it have, it's just a blueprint. Object/instance, is the actual house.

JS: Since there are no classes (the blueprints) but only objects, each object is both the blueprint and the house.

To demonstrate, we can add new methods to objects on the fly.

Javascript:
(note: the class keyword is only syntactic sugar)

function Box(id, label) {
    this.id = id;
    this.label = label;
}

const myBox = new Box(1, "stuff");

function open() {
  return `${this.label} is open`;
}

// add method to "class definition" (object)
Box.prototype.open = open; 

console.log(myBox.open()); // prints: stuff is open

function close() {
  return `${this.label} is closed`;
}

// add method to "class instance" (also object)
myBox.close = close;

console.log(myBox.close()); // prints: stuff is closed
Enter fullscreen mode Exit fullscreen mode

This would not be so easy in python:

class Box(object):
    def __init__(self, id, label):
        self.id = id
        self.label = label 


def open(self):
    return f"{self.label} is open"

myBox = Box(1, 'stuff')

# add method to class definition
Box.open = open 

print(myBox.open()) # prints: stuff is open

def close(self):
    return f"{self.label} is closed"

# add method to class instance (object)? not so fast!
myBox.close = close 

print(myBox.close()) # throws a TypeError: 
# close() missing a required argument
Enter fullscreen mode Exit fullscreen mode

So in python, it is not so easy to add methods to an object (which is an instance of a class) on the fly, but it is possible: .

import types

myBox.close = types.MethodType(close, myBox)
Enter fullscreen mode Exit fullscreen mode

The difference between python and Javascript can be thought of as a difference in approach to solve the OOP concept. Which is what this example illustrates - it's just that you can't do it in python the same way you would in JS.

Okay. One last thing - the concept of pass by value vs. pass by reference. In short - pass by value means that if we have a variable a and then pass it to some function that modifies it, and then look at a again, the changes made inside function will not be reflected. With pass by reference, they will.

Python and JS on the surface, behave the same way:

s = "hello"
def add(arg):
    arg = arg + "bye"
    return arg

add(s) # the returned value doesn't have a name
# bound to it here, but it would be "hellobye"
print(s) # "hello"
Enter fullscreen mode Exit fullscreen mode

and it would behave exactly the same in JS.

let s = "hello";

function add(arg) {
  arg = arg + "bye";
  return arg;
}

add(s); // no assignment to return value
console.log(s); // hello
Enter fullscreen mode Exit fullscreen mode

However the concept behind these two is different. In Python, string is an immutable object. In Javascript, string is a primitive data type, not an object. And primitive data types are immutable.
So passing a primitive data type in JS(or immutable object in Python) to a function, will give you the "pass by value" behavior. And passing an object in JS (or mutable object (e.g. list) in Python) will result in "pass by reference" behavior.

And emphasis on behavior because things are implemented differently in different languages. For example in C/C++, "reference" has a very specific meaning, but in Python, it doesn't exist. More on this here.

Sources:
1 Is Python call by value or call by reference? Neither
2 Why JS is OOP even though it doesn't have classes
3 stack overflow question

Top comments (0)