16.8 Private Class Fields (#) vs TypeScript private

Now, let’s settle a classic TypeScript confusion: the difference between the private keyword and the new JavaScript #private fields. You’ve probably seen both, and if you’re like me, your first thought was, “Great, two ways to do the same thing. Why?” Well, my friend, they are not the same thing, and understanding the distinction is the difference between writing solid, future-proof code and accidentally creating a public API out of your internal state.

16.7 Static Members and Static Blocks

Right, so you’ve got classes. You’ve got instances. But sometimes, you need functionality or data that belongs to the class itself, not any particular instance. That’s where static members come in. Think of them as the class’s own private utilities and global variables, neatly namespaced under the class name. They’re your go-to when you need a factory method, a shared cache, or a constant that’s intrinsic to the class’s identity.

16.6 Implementing Interfaces with implements

Alright, let’s talk about implements. You’ve defined a beautiful, pristine interface. It’s a perfect blueprint, a contract of pure intention. Now, what? You leave it framed on the wall, a theoretical ideal? No. You build the darn thing. That’s where the implements keyword comes in. It’s your way of looking the TypeScript compiler in the eye and saying, “I swear this class will fulfill this contract. Hold me to it.”

16.5 Abstract Classes and Methods

Right, so you’ve got classes. They’re blueprints, they’re cookie cutters, they’re a way to organize your code into neat little bundles of data and functionality. But sometimes, a blueprint is too specific. Sometimes you want to define the general shape of the thing—the fact that it must have doors and windows—but you don’t want to specify what kind of hinges the doors use. You want to leave that crucial, messy detail to the people actually building the house.

16.4 Class Inheritance: extends and super

Right, so you’ve got a class. It’s a lovely little blueprint. But now you want a new class that does everything the first one does, plus some extra stuff, or maybe a slightly different variation. Your first instinct might be to just copy-paste the first class and start hacking away. Don’t. That’s how you create a maintenance nightmare where a bug fix needs to be applied in five different places. This is where extends comes in—it’s TypeScript’s mechanism for classical inheritance, letting you create a new class based on an existing one.

16.3 Methods and Accessors: get and set

Right, let’s talk about giving your classes some action. You’ve got your properties, your blueprints, but a class that just sits there holding data is about as useful as a screen door on a submarine. Methods are the functions inside your class that make things happen, and accessors (get/set) are a clever, sometimes too-clever, way to control access to your properties. They let you dress up a property access as if it’s just a simple field, while you’re secretly running a whole function behind the curtain.

16.2 Constructor Parameter Shorthand

Now, let’s talk about one of those little quality-of-life features in TypeScript that you’ll either use constantly or forget exists entirely: constructor parameter shorthand. It’s a bit of syntactic sugar, but it’s the good kind—the kind that doesn’t rot your teeth and actually saves you from carpal tunnel. Picture this utterly mundane and repetitive scenario. You’re writing a class, and of course, you need to initialize it with some values. The old-school, verbose, Java-esque way looks like this:

16.1 Class Fields: Public, Private, Protected, and Readonly

Right, let’s talk about access modifiers. This is where TypeScript starts to feel less like JavaScript’s quirky cousin and more like a proper, grown-up language with a sense of decorum. It’s the language saying, “Okay, fine, we’ll let you have your dynamic chaos, but at least here, inside this class, we’re going to have some rules.” And thank goodness for that. In vanilla JavaScript, everything is public. Every property you slap onto this is fair game for anyone, anywhere, to poke, prod, and mutate. It’s the equivalent of leaving your diary on a park bench with a sign that says “Please be nice.” TypeScript gives you the tools to lock that diary in a safe, give a key to your best friend, and tell everyone else to get lost.

26.7 Comparing Objects: __eq__, __lt__, and @total_ordering

The Need for Custom Comparisons By default, Python’s == and != operators for objects compare their identities—that is, they check if two variables refer to the exact same object in memory, behaving like the is operator. This is rarely the desired behavior for data-centric classes. For instance, two distinct BankAccount objects with the same account number and balance should be considered equal for most application logic, even though they are separate instances. To enable this value-based comparison, you must provide your own implementation by defining the __eq__ method.

26.6 Object Lifecycle: Creation, __init__, and Destruction

The __init__ Method: The Object Constructor The __init__ method is the most fundamental and frequently used special method in Python. It is not technically a constructor—the actual object creation is handled by the __new__ method—but rather an initializer. After the __new__ method has created a new instance of the class, __init__ is automatically called to initialize the new object’s attributes and put it into a valid initial state. Its purpose is to ensure that every new object starts its life with the necessary data. The first parameter of __init__ is always self, which is a reference to the newly created instance being initialized. Subsequent parameters are used to pass initial values into the object.

26.5 The __dict__ of an Instance and a Class

In Python, every instance and class has a __dict__ attribute, which is a dictionary that stores its writable attributes. This mechanism is the primary way Python implements dynamic attribute storage for objects. Understanding __dict__ is crucial for comprehending how attribute lookup works, how memory is used, and how to perform advanced metaprogramming tasks. The Instance __dict__ When you create an instance of a class, Python allocates a new dictionary to store instance-specific attributes. This is the __dict__ you access directly from the instance. It is the first place the interpreter looks during attribute lookup on an instance.

26.4 String Representations: __str__ and __repr__

In Python, every object can have two distinct string representations: one for informal, human-readable output and another for formal, unambiguous debugging and development. These representations are provided by the __str__ and __repr__ special methods, respectively. Understanding the difference between them and implementing them correctly is crucial for creating robust, debuggable classes. The Core Distinction: str vs. repr The fundamental difference lies in their intended audience and purpose. The __str__ method is called by the str() built-in function and by the print() function. Its goal is to return a string that is “nicely printable” and easily understood by an end-user. It is meant to be informal and concise.

26.3 Methods: Instance, Class, and Static

In object-oriented programming, methods define the behaviors and actions that objects of a class can perform. Python provides three distinct types of methods, each with a different purpose, scope, and relationship to the class and its instances. Understanding the distinction between them is crucial for designing well-structured, efficient, and maintainable code. Instance Methods The most common type of method is the instance method. By default, any method defined inside a class is an instance method. Its defining characteristic is that its first parameter is always self, which is a reference to the specific instance of the class that called the method. Through self, the method can access and modify the instance’s attributes and call other instance methods.

26.2 Instance Attributes vs Class Attributes

In Python, both instance attributes and class attributes are fundamental to object-oriented programming, but they serve distinct purposes and behave differently. Understanding their distinction is crucial for designing robust and predictable classes. Definition and Basic Syntax An instance attribute is a variable that belongs to a specific, individual object (an instance) of a class. Its value is unique to that instance. You typically define instance attributes inside the __init__ method using self.

26.1 Defining a Class: class, __init__, and self

In Python, a class serves as a blueprint for creating objects. Objects are instances of a class, encapsulating both data (attributes) and behaviors (methods) that are logically related. The class keyword is the fundamental building block for this object-oriented paradigm, allowing you to define a new data type with its own specific structure and functionality. The class Keyword and Basic Structure The process of creating a new class begins with the class keyword, followed by the name of the class (by convention, using CamelCase) and a colon. The body of the class, indented beneath, contains all the method definitions that define the class’s behavior. The simplest possible class is one with no body at all, though it’s not particularly useful.

— joke —

...