Object Orientation

Python Self — An Interactive Guide with Video

Many beginners who just start out with object-oriented programming in Python are confused about the purpose of the keyword “self”. Let’s resolve this confusion once and for all! The name self is, by convention, the name of the first argument of a Python class method. The variable self points to the concrete instance on which …

Python Self — An Interactive Guide with Video Read More »

List Changes After Assignment — How to Clone or Copy It?

Problem: If you assign a list object to a new variable using new_list = old_list, any modification to new_list changes old_list. What’s the reason for this and how can you clone or copy the list to prevent this problem? Example: Let’s consider the following example. Appending an element to the new_list also modifies the original …

List Changes After Assignment — How to Clone or Copy It? Read More »

Python One Line Class

Do you hate those lengthy class definitions with __init__ and too many whitespaces and newlines? Python One-Liners to the rescue! Luckily, you can create classes in a single line—and it can even be Pythonic to do so! Sounds too good to be true? Let’s dive right into it! Problem: How to create a Python class …

Python One Line Class Read More »

Python raw_input() vs input()

Summary: The key differences between raw_input() and input() functions are the following: raw_input() can be used only in Python 2.x and is obsolete in Python 3.x and above and has been renamed input() In Python 2.x, raw_input() returns a string whereas input() returns result of an evaluation. While in Python 3.x input() returns a string …

Python raw_input() vs input() Read More »

How to Access an Object Attribute Given the Attribute Name as a String?

You may know the following problem: You have an object’s attribute name as a string—say, you’ve read it from a file—and you want to access the attribute with the given name. But you cannot use the syntax object.”attribute” because the dot notation only allows for name-access like this: object.attribute. How do you resolve this problem? …

How to Access an Object Attribute Given the Attribute Name as a String? Read More »

How To Ask Users For Input Until They Provide a Valid Input?

Summary: To accept valid inputs from the user either use a While Loop With Custom Validations or use the PyInputPlus module to avoid tedious validation definitions. You can also use recursion to prompt the user for entering the valid input. Overview Problem: Given a user input; accept the input only if it is valid otherwise …

How To Ask Users For Input Until They Provide a Valid Input? Read More »

What Does “if __name__ == ‘__main__'” Do in Python?

Today, let’s discuss something that’s all over the place in many code bases: what does if __name__ == ‘__main__’ do in Python? The statement if __name__ == ‘__main__’: checks if variable __name__ is set to the string value ‘__main__’ which holds only within the main source file from which you initially execute your code. In all other …

What Does “if __name__ == ‘__main__’” Do in Python? Read More »

[Python OOP Cheat Sheet] A Simple Overview of Object-Oriented Programming

Q: What’s the object-oriented way to become wealthy? A: Inheritance. 😒 Your vocabulary determines the reality of your life. In this tutorial, I have compiled the most essential terms and concepts of object-oriented programming in Python. My goal was to create the best Python OOP cheat sheet that shows them in one place. Well — …

[Python OOP Cheat Sheet] A Simple Overview of Object-Oriented Programming Read More »

Python Join List of Bytes (and What’s a Python Byte Anyway?)

When teaching the Finxter Python students, I’m often shocked how even intermediate-level coders do not understand the very basic foundations of computer science. In CS, there’s almost nothing as fundamental as a simple, plain byte. (You know, that sequence of 0s and 1s called bits). This tutorial aims to clarify some misconceptions and questions regarding …

Python Join List of Bytes (and What’s a Python Byte Anyway?) Read More »

What is __ doc __ in Python?

Python objects have an attribute called __doc__ that provides a documentation of the object. For example, you simply call Dog.__doc__ on your class Dog to retrieve its documentation as a string. You can define the docstring using a string surrounded by triple quotes as shown in the example: Try it yourself in the interactive Python …

What is __ doc __ in Python? Read More »