DEV Community

VICTOR OMONDI
VICTOR OMONDI

Posted on

Static methods in Python vs Javascript

Python

class User():
    def __init__(self, username):
        self.username = username

    @staticmethod
    def what_am_i():
        return 'I can be called by User class and also by any User instance'

User.what_am_i()
Enter fullscreen mode Exit fullscreen mode

Javascript

class User {
    constructor(username) {
        this.username = username;
    }

    static whatAmI() {
        return 'I can be called only by User class'
    }
}

User.whatAmI();
Enter fullscreen mode Exit fullscreen mode

Thing to note

In python if we declare a static method it can be called by both the class and by any of the instances (objects) created from the class ✔️✔️✔️

Whereas in Javascript though, if we declare any static methods, it can only be accessed by calling it from the class and not by its instances 📣✔️✔️✔️

Top comments (0)