杰瑞科技汇

python instance of

Of course! The Python is operator is a fundamental tool for checking object identity. It's crucial to understand how it works, especially when you're dealing with objects like lists, dictionaries, and custom classes.

python instance of-图1
(图片来源网络,侵删)

Let's break it down.

The Short Answer: What is Does

The is operator checks if two variables point to the exact same object in memory. It answers the question: "Are these two references referring to one and the same thing?"

Think of it like this:

  • is is like asking, "Are you the exact same person?"
  • (the equality operator) is like asking, "Do you have the same name and address?"

is vs. : The Core Difference

This is the most important distinction to understand.

python instance of-图2
(图片来源网络,侵删)
  • is: Compares identity (memory address). a is b is True if a and b are the exact same object.
  • Compares equality (value). a == b is True if the objects a and b contain the same value.

Let's see this with numbers and strings, which are a special case.

Example with Numbers (Integers)

For small integers (usually from -5 to 256), Python caches them for performance. So, variables assigned the same value often point to the same object.

a = 256
b = 256
print(f"a is b: {a is b}")      # True: They point to the same cached object
print(f"a == b: {a == b}")      # True: Their values are also equal
print("-" * 20)
c = 300
d = 300
print(f"c is d: {c is d}")      # False: They are different objects in memory
print(f"c == d: {c == d}")      # True: But their values are equal

Output:

a is b: True
a == b: True
--------------------
c is d: False
c == d: True

Example with Strings (String Interning)

Python also "interns" strings, meaning it reuses the same string object in memory if the string is a valid identifier or looks like one.

python instance of-图3
(图片来源网络,侵删)
e = "hello"
f = "hello"
print(f"e is f: {e is f}")      # True: Python interns these strings
print(f"e == f: {e == f}")      # True: Their values are equal
print("-" * 20)
g = "hello world"
h = "hello world"
print(f"g is h: {g is h}")      # Often True, but not guaranteed in all cases.
                                # It depends on the Python implementation and string content.
print(f"g == h: {g == h}")      # True: Their values are equal

Example with Mutable Objects (Lists, Dictionaries)

This is where is becomes critical. Lists are mutable, meaning they can be changed. Python creates a new list object each time you use the [] syntax.

list1 = [1, 2, 3]
list2 = [1, 2, 3]
print(f"list1 is list2: {list1 is list2}")  # False: They are two separate list objects
print(f"list1 == list2: {list1 == list2}")  # True: Their contents (values) are identical
# Now, let's make them point to the same object
list3 = list1
print(f"list1 is list3: {list1 is list3}")  # True: list3 is just another name for list1
print(f"list1 == list3: {list1 == list3}")  # True: Their values are also identical

Output:

list1 is list2: False
list1 == list2: True
--------------------
list1 is list3: True
list1 == list3: True

The Singleton: None

The most common and important use case for is is checking for None. None is a singleton object, meaning there is only ever one None instance in a Python program. You should always use is to check for None.

my_variable = None
# Correct way
if my_variable is None:
    print("my_variable is indeed None.")
# Incorrect (but often works) way
# It's better to avoid this because it's comparing values, not identity.
# It could theoretically break if a class defined __eq__ to return True when compared to None.
if my_variable == None:
    print("This also works, but 'is' is the preferred, more explicit way.")

Best Practice: Use is None and is not None.


Checking Class Instance Types (isinstance)

You might have confused is with isinstance(). They are related but serve different purposes.

  • isinstance(obj, class_or_tuple): Checks if an object is an instance of a class or a subclass thereof. This is the correct way to perform type checking.

Let's create a custom class hierarchy to see this in action.

class Animal:
    pass
class Dog(Animal):
    pass
class Cat(Animal):
    pass
my_dog = Dog()
my_cat = Cat()
my_animal = Animal()
# Check if my_dog is an instance of Dog
print(f"Is my_dog a Dog? {isinstance(my_dog, Dog)}")      # True
# Check if my_dog is an instance of its parent class, Animal
print(f"Is my_dog an Animal? {isinstance(my_dog, Animal)}") # True, because Dog inherits from Animal
# Check if my_cat is an instance of Dog
print(f"Is my_cat a Dog? {isinstance(my_cat, Dog)}")      # False
# Check if my_animal is an instance of Dog
print(f"Is my_animal a Dog? {isinstance(my_animal, Dog)}") # False
# You can also check against a tuple of types
print(f"Is my_dog a Dog or a Cat? {isinstance(my_dog, (Dog, Cat))}") # True

Why isinstance is better than type(obj) is ...

isinstance correctly handles inheritance. The type() function does not.

# Using type() - This is often not what you want
print(f"type(my_dog) is Dog: {type(my_dog) is Dog}")        # True
print(f"type(my_dog) is Animal: {type(my_dog) is Animal}")  # False! This is a problem.
# Using isinstance - This is the correct way for type checking
print(f"isinstance(my_dog, Animal): {isinstance(my_dog, Animal)}") # True, as expected.

Summary Table

Concept Operator What it Checks When to Use
Identity is Do two variables point to the exact same object in memory? - Checking for None (if x is None).
- Checking if two variables are aliases for the same object.
- Performance-sensitive code (though rare).
Equality Do two objects have the same value? - Almost all other cases.
- Comparing numbers, strings, lists, dictionaries, etc.
- When you care about content, not memory address.
Instance Type isinstance() Is an object an instance of a class (or its subclass)? - Type checking in functions.
- Polymorphic code (writing functions that work with a base class).

Final Rule of Thumb

  1. Use is when you care about object identity. The primary case is if my_variable is None:.
  2. Use when you care about object value. This is for 99% of your comparisons.
  3. Use isinstance() for type checking, especially when dealing with inheritance.
分享:
扫描分享到社交APP
上一篇
下一篇