杰瑞科技汇

Python中object的new方法到底有何特殊作用?

Of course! The phrase "python object new" can mean a few different things depending on your context. Let's break it down from the most common interpretation to the more technical one.

The Most Common Meaning: Creating a New Instance of a Class

This is what 99% of Python developers mean when they talk about "making a new object." You do this by calling a class, just like you would call a function.

Here’s a step-by-step guide.

Step 1: Define a Class (The Blueprint)

A class is a blueprint for creating objects. It defines the attributes (data) and methods (functions) that the objects created from it will have.

# Define a simple 'Dog' class
class Dog:
    # This is the constructor method, called when a new object is created.
    # 'self' refers to the specific instance of the object being created.
    def __init__(self, name, age, breed):
        print(f"Creating a new Dog object for {name}!")
        # These are attributes (variables) belonging to the object
        self.name = name
        self.age = age
        self.breed = breed
    # This is a method (a function belonging to the object)
    def bark(self):
        return f"{self.name} says: Woof!"
    def celebrate_birthday(self):
        self.age += 1
        print(f"Happy Birthday, {self.name}! You are now {self.age} years old.")

Step 2: Create an Instance (The Object)

To create a new object (an instance) from the class, you "call" the class name.

# Create a new instance of the Dog class
dog1 = Dog("Rex", 5, "German Shepherd")
# Create another instance of the Dog class
dog2 = Dog("Bella", 2, "Golden Retriever")

What happens when you run this?

  1. Python sees Dog(...).
  2. It looks for the __init__ method inside the Dog class.
  3. It automatically creates a new object (an instance of Dog).
  4. It passes the new object as the first argument (self).
  5. It passes the other arguments ("Rex", 5, "German Shepherd") to the __init__ method.
  6. The __init__ method sets the name, age, and breed attributes on the new object.

You now have two distinct Dog objects, dog1 and dog2, each with their own data.

Step 3: Use the Object

You can access the object's attributes and methods using the dot () operator.

# Access attributes
print(f"{dog1.name} is a {dog1.breed}.")
# Output: Rex is a German Shepherd.
print(f"{dog2.name} is {dog2.age} years old.")
# Output: Bella is 2 years old.
# Call methods
print(dog1.bark())
# Output: Rex says: Woof!
dog2.celebrate_birthday()
# Output: Happy Birthday, Bella! You are now 3 years old.
# The attribute on dog2 has changed
print(f"{dog2.name} is now {dog2.age} years old.")
# Output: Bella is now 3 years old.

The Technical Meaning: The __new__ Method

Under the hood, Python uses two special methods to create an object:

  1. __new__(): Responsible for creating the instance (the raw memory allocation).
  2. __init__(): Responsible for initializing the instance (setting up its attributes).

You almost never need to override __new__(). It's primarily used for advanced cases like:

  • Creating subclasses of immutable types (like int, str, tuple).
  • Implementing Singleton patterns (a class that can only have one instance).
  • Metaprogramming.

Let's look at a simple example to understand the difference.

The Default __new__ (What you always get)

When you define a class without __new__, Python provides a default one that looks something like this:

class SimpleObject:
    def __init__(self, value):
        print("  -> __init__ called. Initializing the object.")
        self.value = value
    # You don't see this, but Python does it for you:
    # def __new__(cls, *args, **kwargs):
    #     print("  -> __new__ called. Creating the object instance.")
    #     # 1. Create the instance using the base class (object) __new__
    #     instance = super().__new__(cls)
    #     # 2. Return the instance to be initialized by __init__
    #     return instance
print("Creating a SimpleObject...")
so = SimpleObject(10)

Output:

Creating a SimpleObject...
  -> __new__ called. Creating the object instance.  # (Implicit)
  -> __init__ called. Initializing the object.

Overriding __new__ (A Singleton Example)

Here's a practical example where we override __new__ to create a Singleton. No matter how many times you try to create an object, you'll get the same one.

class Singleton:
    _instance = None  # Class attribute to hold the single instance
    def __new__(cls, *args, **kwargs):
        # If an instance doesn't exist yet...
        if not cls._instance:
            print(f"  -> __new__: Creating the one and only instance of {cls.__name__}.")
            # ...create one using the parent's __new__ method
            cls._instance = super().__new__(cls)
        else:
            print(f"  -> __new__: Instance already exists. Returning the existing one.")
        # Return the instance (either newly created or the existing one)
        return cls._instance
    def __init__(self, data):
        # This check is important to prevent re-initialization
        if not hasattr(self, 'initialized'):
            print(f"  -> __init__: Initializing the instance with data: {data}")
            self.data = data
            self.initialized = True
# --- Let's use it ---
print("\n--- First Attempt ---")
s1 = Singleton("First Data")
print("\n--- Second Attempt ---")
s2 = Singleton("Second Data") # This data will be ignored!
print("\n--- Are they the same object? ---")
print(s1 is s2)  # Output: True
print(f"s1.data: {s1.data}") # Output: s1.data: First Data
print(f"s2.data: {s2.data}") # Output: s2.data: First Data

Output:

--- First Attempt ---
  -> __new__: Creating the one and only instance of Singleton.
  -> __init__: Initializing the instance with data: First Data
--- Second Attempt ---
  -> __new__: Instance already exists. Returning the existing one.
  -> __init__: Initializing the instance with data: Second Data
# Notice __init__ is called again, but our check prevents the data from being changed.
--- Are they the same object? ---
True
s1.data: First Data
s2.data: First Data

Summary Table

Concept What it is When to use it Example
Creating an Instance The common act of making an object from a class. Almost always. This is the fundamental way you work with object-oriented Python. my_list = list() or dog = Dog("Fido", 3)
__init__ method The initializer. Sets up the object's state after it's created. Very often. Use this to set up your object's attributes. def __init__(self, name): self.name = name
__new__ method The creator. Allocates memory and returns the raw instance. Very rarely. For special cases like Singletons or subclassing immutable types. def __new__(cls): ... return super().__new__(cls)
分享:
扫描分享到社交APP
上一篇
下一篇