Python樹林– category –
-
Python樹林
Re-raising Exceptions in Python: Logging Errors and Notifying the Caller
When implementing exception handling (try-except), there are cases where you don't want to "resolve the error on the spot," but rather "log the fact that an error occurred and leave the actual handling to the caller (higher-level process... -
Python樹林
Intentionally Raising Exceptions in Python: Using the raise Statement and Checking Input Values
Exception handling (try-except) is a feature for "catching" errors that occur. However, to create robust programs, there are times when you, as a developer, must intentionally "raise" an error. For example, if unexpected data is passed t... -
Python樹林
Python Exception Handling: Controlling Cleanup with else and finally
Python's exception handling syntax (try-except) has two lesser-known but very important optional clauses: else and finally. By combining these appropriately, you can clearly define "processing to execute only when no error occurs" and "c... -
Python樹林
Python Exception Handling: How to Catch Multiple Errors Individually with try-except
In programming, more than one type of error (exception) can occur within a single block of code. For example, data loading might fail because "file not found," "invalid data format," or "missing required value." Python's try-except state... -
Python樹林
Common Python Exceptions: Causes and Solutions for Frequent Errors
When programming in Python, you will encounter various "exceptions (errors)." While error messages might look difficult at first glance, they are actually important hints that tell you exactly "what is wrong." Knowing the causes of each ... -
Python樹林
Python Exception Handling: Basics and Practice of Error Handling with try-except
When executing a program, errors can occur due to unexpected data input or missing files. In Python, these runtime errors are called "Exceptions." Normally, when an exception occurs, the program terminates immediately. However, by implem... -
Python樹林
Creating Custom Python Iterators: Implementing iter and next Methods
Python's for loops can iterate not only over lists and dictionaries but over any "iterable" object. The mechanism working behind this "iterable" concept is the "Iterator." By implementing specific special methods in your own classes, you... -
Python樹林
Customizing Python Classes: Implementing Subscript Access with __getitem__ and Other Special Methods
Sometimes you want to use subscript operations (bracket notation) like obj[0] or obj["key"] on your custom classes, just like with Python lists and dictionaries. To achieve this, you need to implement specific special methods within the ... -
Python樹林
Overloading Comparison Operators in Python: Defining Magnitude Relationships (<, <=, ==, etc.) for Custom Classes
Just as you can compare numbers or strings with a < b or a == b, you may want to define magnitude relationships or equality for your own custom classes (objects). For example, judging "Version 1.2 is older (smaller) than 1.5" in a cla... -
Python樹林
Operator Overloading in Python: Defining Arithmetic Operations (+, -, *, /) in Custom Classes
In Python, just as you can use operators like + and - with numeric types (int and float), you can also define the behavior of these operators for classes (objects) you create yourself. This is called "Operator Overloading." For example, ... -
Python樹林
Introduction to Python Special Methods (Magic Methods): Customizing Classes with __init__ and __str__
When defining a class in Python, you often see methods surrounded by double underscores (e.g., __init__). These are called Special Methods or Magic Methods. By defining special methods, you can make your custom classes behave like Python... -
Python樹林
How to Check Variable Types in Python: Differences and Usage of type() and isinstance()
Python is a "dynamically typed language," meaning you don't need to specify types when declaring variables. Therefore, you might encounter situations where you want to check "what kind of data is currently in this variable" or perform co... -
Python樹林
Investigating Python Object Attributes: How to Use dir() and hasattr() Functions
When developing in Python, you often want to check what methods a library object has, or determine if a specific variable exists in a dynamically generated object. To perform this kind of "Object Introspection," Python provides two conve... -
Python樹林
String Representation of Python Objects: Differences and Customization of __str__ and __repr__
When you define a custom class in Python and print its instance using the print() function, by default, internal information (memory address) like <__main__.ClassName object at 0x...> is displayed, which is hard for humans to under... -
Python樹林
“Private” Variables in Python: Double Underscore (__) and Name Mangling
Languages like Java and C++ have access modifiers (private, protected, public) to prohibit external access to class members. Python, however, does not have "private" variables in the strict sense (variables that are completely inaccessib... -
Python樹林
Python’s 3 Method Types: Differences and Usage of Instance, Class, and Static Methods
In Python classes, methods differ in their "first argument" and "how they are called." You need to use them appropriately based on their role. Instance Method: Manipulates data for individual objects (instances). Class Method: Manipulate... -
Python樹林
Python Class Variables vs. Instance Variables: Differences and How to Update Correctly (Beware of Shadowing)
In Python classes, there are two main types of variables: "Class Variables" and "Instance Variables." These differ fundamentally in where they are defined, their scope, and how data is held. A common trap for Python beginners is the beha... -
Python樹林
Python Class Inheritance: How to Extend Parent Class Functionality Using super()
In programming, the mechanism of creating a new class by taking over the features of an existing class (blueprint) while adding new features or modifying parts of it is called "Inheritance." By using inheritance, you avoid writing common... -
Python樹林
Python Classes and Object-Oriented Programming: How to Define Custom Data Structures
Python supports "Object-Oriented Programming." Until now, we have used standard data types like lists and dictionaries. However, as programs grow larger, you often want to manage related data (variables) and the processing (functions) th... -
Python樹林
Python Generator Functions and yield: Implementing Memory-Efficient Iteration
In Python, objects that allow you to "extract multiple elements in order," like lists, are called iterables. Among them, "Generators" have a special and powerful feature. While lists expand all elements into memory at once, generators re... -
Python樹林
Python lambda Expressions: Writing Anonymous Functions in One Line
In Python, besides defining functions using the def keyword, there is a way to create "nameless functions (anonymous functions)" using lambda expressions. Using lambda expressions allows you to write very short functions in a single line... -
Python樹林
Introduction to Python Decorators: Extending Functionality with the @ Syntax
Python Decorators allow you to add specific processing before or after the execution of a function without changing the function's code directly. They are very useful when implementing processing that you want to apply to multiple functi... -
Python樹林
Python Closures: How to Retain State in Functions and the nonlocal Keyword
In Python, a "Closure" is an important concept in functional programming. Simply put, it refers to "a function that continues to hold variables from the environment (scope) in which it was defined." Normally, local variables defined with... -
Python樹林
Python Inner Functions (Nested Functions): Benefits and Usage of Defining Functions Inside Functions
In Python, you can define a function inside another function. This is called an "Inner Function" or "Nested Function." At first glance, it might seem complicated, but it is a very effective technique when you want to reuse specific proce... -
Python樹林
Python Functions are First-Class Objects: Assignment and Passing as Arguments
In Python, functions are treated as "First-Class Objects." This means that functions can be treated just like other data types such as numbers or strings: they can be assigned to variables, passed as arguments to other functions, or stor... -
Python樹林
Python Global vs. Local Variables: Handling External Variables and the global Keyword
In programming, the range in which a variable can be referenced and remains valid is called its "Scope." In Python, there are specific rules when handling variables defined outside a function (Global/Module variables) from within a funct... -
Python樹林
Returning Multiple Values from Python Functions: Tuple Auto-Packing and Unpacking
In many programming languages, a function can basically return only one value. If you want to return multiple values, you usually have to pack them into an array or an object. However, in Python, you can easily return multiple values sim... -
Python樹林
Python Function Default Arguments: Basics and Why You Should Avoid Mutable Defaults
When defining a function, setting a value for an argument beforehand allows you to omit that argument when calling the function. This is called a "Default Argument." While this is a very convenient feature, you need to be careful with Py... -
Python樹林
Unpacking Argument Dictionaries in Python: How to Pass Dictionaries as Keyword Arguments (** Unpacking)
In Python function calls, there is a feature that allows you to pass data stored in a dictionary (dict) as "keyword arguments" all at once. This uses double asterisks **. It acts as the counterpart to *, which unpacks lists or tuples as ... -
Python樹林
Unpacking Argument Lists in Python: How to Pass Lists and Tuples using *
When passing arguments to a function, sometimes your data is already grouped in a list or a tuple. Normally, you would need to extract elements from the list one by one using indices to specify them as arguments. However, in Python, usin...