Object Orientation

What Are Differences Between type() and isinstance()?

The main difference between type() and isinstance() is that type(object) returns the type of an object and isinstance(object, class) returns True if the object argument is an instance of the class argument or in a direct or indirect subclass relationship. To strengthen your understanding, let’s quickly recap the syntactical definitions of both functions: type(object) โ€“ …

What Are Differences Between type() and isinstance()? Read More »

[Solved] TypeError: method() takes 1 positional argument but 2 were given

From teaching hundreds of thousands of students Python, I found this error to be a classic. I think understanding classes is hard enough, but many coders who’ve just started to learn about Python are rightly confused ๐Ÿ˜– about the TypeError that complains about too few positional arguments. Let’s resolve this confusion once and for all, …

[Solved] TypeError: method() takes 1 positional argument but 2 were given Read More »

Python Method Resolution Order (MRO)

Today we’re going to look at the Python Method Resolution Order or MRO for short. If you’ve been following the tutorials on Python classes and inheritance, and you’ve been practicing in code, you’ll understand that once the hierarchy of classes moves into multiple inheritances, you may return strange results or end up with incomprehensible errors. …

Python Method Resolution Order (MRO) Read More »

An Introduction To Python Classes โ€“ Inheritance, Encapsulation, and Polymorphism

This article continues from Introduction to Classes – Part One, where we explained what classes are, their components, and why we use them. We also looked at some unique characteristics of classes that assist us in creating cleaner code. If you haven’t read Part One and are new to classes, I suggest reading that introduction …

An Introduction To Python Classes โ€“ Inheritance, Encapsulation, and Polymorphism Read More »

Method Overriding vs Overloading in Python [+Video]

Method overriding vs overloading in Python—what’s the difference? If you’re short on time—here it is: Method overloading: creating a method that can be called with different arguments such as m() and m(1, 2, 3). Method overriding: overwriting the functionality of a method defined in a parent class. In method overloading, methods in a given class …

Method Overriding vs Overloading in Python [+Video] Read More »

How to Get a Function Name as a String in Python?

Problem Formulation Given a function object assigned to a name. How to get the name of the function as a string? For example, consider the following function your_function. How to get the name “your_function” from this? Your desired value of the result stored in string_name is the string “your_function”. Method 1: Use the __name__ Attribute …

How to Get a Function Name as a String in Python? Read More »