Hướng dẫn how to check if an object is an instance of a class python - cách kiểm tra xem một đối tượng có phải là một phiên bản của một lớp python không

Hàm Python từ

num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
2 kiểm tra xem đối tượng hoặc biến có phải là một thể hiện của loại lớp hoặc loại dữ liệu được chỉ định hay không.

Show

Ví dụ:

num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
3 kiểm tra xem
num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
4 là một ví dụ của lớp
num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
5.

Ngoài ra, giải quyết: Bài tập cơ bản Python và người mới bắt đầu đố: Python Basic Exercise and Beginners Quiz

Cách sử dụng hàm isInstance () trong python

Hãy cùng xem cú pháp trước khi chuyển sang ví dụ.

Syntax::

isinstance(object, classinfo)
  • Phải mất hai đối số, và cả hai đều là bắt buộc.mandatory.
  • Hàm
    num = 90
    result = isinstance(num, int)
    if result:
        print("Yes")
    else:
        print("No")
    2 kiểm tra xem đối số
    num = 90
    result = isinstance(num, int)
    if result:
        print("Yes")
    else:
        print("No")
    7 là một thể hiện hoặc lớp con của đối số lớp
    num = 90
    result = isinstance(num, int)
    if result:
        print("Yes")
    else:
        print("No")
    8

Hướng dẫn how to check if an object is an instance of a class python - cách kiểm tra xem một đối tượng có phải là một phiên bản của một lớp python không
Python isinstance ()

Sử dụng hàm

num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
2, chúng ta có thể kiểm tra xem một đối tượng/biến có phải là một thể hiện của loại hoặc lớp được chỉ định như int hoặc danh sách hay không. Trong trường hợp kế thừa, chúng ta có thể kiểm tra xem lớp được chỉ định có phải là lớp cha của một đối tượng không.

  1. Pass đối tượng đến isinstance ()

    Chuyển biến bạn muốn kiểm tra dưới dạng đối số

    num = 90
    result = isinstance(num, int)
    if result:
        print("Yes")
    else:
        print("No")
    7 cho
    num = 90
    result = isinstance(num, int)
    if result:
        print("Yes")
    else:
        print("No")
    2. Ở đây đối tượng có thể là bất kỳ đối tượng lớp hoặc bất kỳ tên biến nào

  2. Chỉ định tên loại hoặc loại là đối số
    num = 90
    result = isinstance(num, int)
    if result:
        print("Yes")
    else:
        print("No")
    8

    Ví dụ:

    Yes
    3 để kiểm tra xem
    Yes
    4 có phải là một thể hiện của lớp ____ 25 .________ 18 là tên loại hoặc tên lớp bạn muốn kiểm tra đối với biến. Ở đây bạn có thể chỉ định tên loại dữ liệu hoặc tên lớp. Bạn cũng có thể vượt qua nhiều lớp/loại ở định dạng tuple. Ví dụ: bạn có thể vượt qua
    Yes
    5,
    num = 90
    result = isinstance(num, int)
    if result:
        print("Yes")
    else:
        print("No")
    5,
    Yes
    9,
    # Check if 80 is an instance of class int
    number = 80
    print(isinstance(number, int))
    # output True
    
    print(isinstance(number, float))
    # output False
    
    pi = 3.14
    # Check 3.14 is an instance of class float
    print(isinstance(pi, float))
    # Output True
    
    # Check if (1 + 2j) is an instance of complex
    complex_num = 1 + 2j
    print(isinstance(complex_num, complex))
    # Output True
    
    # Check if 'PYnative' is an instance of class string
    name = "PYnative.com"
    print(isinstance(name, str))
    # Output True
    
    # Check if names is an instance of class list
    names = ["Eric", "Scott", "Kelly"]
    print(isinstance(names, list))
    # Output True
    
    # Check if student_report is an instance of class dict
    student_report = {"John": 80, "Eric": 70, "Donald": 90}
    print(isinstance(student_report, dict))
    # Output True
    
    # Check if names is an instance of class tuple
    names = ("Sam", "Kelly", 'Emma')
    print(isinstance(names, tuple))
    # Output True
    
    # Check if numbers is an instance of class tuple
    numbers = {11, 22, 33, 44, 55}
    print(isinstance(numbers, set))
    # Output True
    0 hoặc bất kỳ lớp do người dùng tạo.
    num = 90
    result = isinstance(num, int)
    if result:
        print("Yes")
    else:
        print("No")
    8 is a type name or Class name you want to check against the variable. Here you can specify data type name or Class name.
    You can also pass multiple classes/types in a tuple format. For example, you can pass
    Yes
    5,
    num = 90
    result = isinstance(num, int)
    if result:
        print("Yes")
    else:
        print("No")
    5,
    Yes
    9,
    # Check if 80 is an instance of class int
    number = 80
    print(isinstance(number, int))
    # output True
    
    print(isinstance(number, float))
    # output False
    
    pi = 3.14
    # Check 3.14 is an instance of class float
    print(isinstance(pi, float))
    # Output True
    
    # Check if (1 + 2j) is an instance of complex
    complex_num = 1 + 2j
    print(isinstance(complex_num, complex))
    # Output True
    
    # Check if 'PYnative' is an instance of class string
    name = "PYnative.com"
    print(isinstance(name, str))
    # Output True
    
    # Check if names is an instance of class list
    names = ["Eric", "Scott", "Kelly"]
    print(isinstance(names, list))
    # Output True
    
    # Check if student_report is an instance of class dict
    student_report = {"John": 80, "Eric": 70, "Donald": 90}
    print(isinstance(student_report, dict))
    # Output True
    
    # Check if names is an instance of class tuple
    names = ("Sam", "Kelly", 'Emma')
    print(isinstance(names, tuple))
    # Output True
    
    # Check if numbers is an instance of class tuple
    numbers = {11, 22, 33, 44, 55}
    print(isinstance(numbers, set))
    # Output True
    0, or any user-created class.

  3. Thực hiện thao tác của bạn, nếu kết quả là đúng

    num = 90
    result = isinstance(num, int)
    if result:
        print("Yes")
    else:
        print("No")
    2 trả về true nếu một đối tượng hoặc biến thuộc loại được chỉ định khác.True if an object or variable is of a specified type otherwise False.

Thí dụ

Sử dụng isIntance (), chúng ta có thể xác minh xem một biến là số hoặc chuỗi. Hãy cùng giả sử biến

# Check if 80 is an instance of class int
number = 80
print(isinstance(number, int))
# output True

print(isinstance(number, float))
# output False

pi = 3.14
# Check 3.14 is an instance of class float
print(isinstance(pi, float))
# Output True

# Check if (1 + 2j) is an instance of complex
complex_num = 1 + 2j
print(isinstance(complex_num, complex))
# Output True

# Check if 'PYnative' is an instance of class string
name = "PYnative.com"
print(isinstance(name, str))
# Output True

# Check if names is an instance of class list
names = ["Eric", "Scott", "Kelly"]
print(isinstance(names, list))
# Output True

# Check if student_report is an instance of class dict
student_report = {"John": 80, "Eric": 70, "Donald": 90}
print(isinstance(student_report, dict))
# Output True

# Check if names is an instance of class tuple
names = ("Sam", "Kelly", 'Emma')
print(isinstance(names, tuple))
# Output True

# Check if numbers is an instance of class tuple
numbers = {11, 22, 33, 44, 55}
print(isinstance(numbers, set))
# Output True
2 và bạn muốn kiểm tra xem Num có phải là một thể hiện của loại INT hay không.

num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")

Output::

Yes

Như chúng ta có thể thấy trong đầu ra,

num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
2 đã trả lại
# Check if 80 is an instance of class int
number = 80
print(isinstance(number, int))
# output True

print(isinstance(number, float))
# output False

pi = 3.14
# Check 3.14 is an instance of class float
print(isinstance(pi, float))
# Output True

# Check if (1 + 2j) is an instance of complex
complex_num = 1 + 2j
print(isinstance(complex_num, complex))
# Output True

# Check if 'PYnative' is an instance of class string
name = "PYnative.com"
print(isinstance(name, str))
# Output True

# Check if names is an instance of class list
names = ["Eric", "Scott", "Kelly"]
print(isinstance(names, list))
# Output True

# Check if student_report is an instance of class dict
student_report = {"John": 80, "Eric": 70, "Donald": 90}
print(isinstance(student_report, dict))
# Output True

# Check if names is an instance of class tuple
names = ("Sam", "Kelly", 'Emma')
print(isinstance(names, tuple))
# Output True

# Check if numbers is an instance of class tuple
numbers = {11, 22, 33, 44, 55}
print(isinstance(numbers, set))
# Output True
4 vì
# Check if 80 is an instance of class int
number = 80
print(isinstance(number, int))
# output True

print(isinstance(number, float))
# output False

pi = 3.14
# Check 3.14 is an instance of class float
print(isinstance(pi, float))
# Output True

# Check if (1 + 2j) is an instance of complex
complex_num = 1 + 2j
print(isinstance(complex_num, complex))
# Output True

# Check if 'PYnative' is an instance of class string
name = "PYnative.com"
print(isinstance(name, str))
# Output True

# Check if names is an instance of class list
names = ["Eric", "Scott", "Kelly"]
print(isinstance(names, list))
# Output True

# Check if student_report is an instance of class dict
student_report = {"John": 80, "Eric": 70, "Donald": 90}
print(isinstance(student_report, dict))
# Output True

# Check if names is an instance of class tuple
names = ("Sam", "Kelly", 'Emma')
print(isinstance(names, tuple))
# Output True

# Check if numbers is an instance of class tuple
numbers = {11, 22, 33, 44, 55}
print(isinstance(numbers, set))
# Output True
5 giữ giá trị số nguyên.

Lưu ý: Nếu đối số

num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
8 không phải là một loại, loại hoặc tuple loại, thì ngoại lệ
# Check if 80 is an instance of class int
number = 80
print(isinstance(number, int))
# output True

print(isinstance(number, float))
# output False

pi = 3.14
# Check 3.14 is an instance of class float
print(isinstance(pi, float))
# Output True

# Check if (1 + 2j) is an instance of complex
complex_num = 1 + 2j
print(isinstance(complex_num, complex))
# Output True

# Check if 'PYnative' is an instance of class string
name = "PYnative.com"
print(isinstance(name, str))
# Output True

# Check if names is an instance of class list
names = ["Eric", "Scott", "Kelly"]
print(isinstance(names, list))
# Output True

# Check if student_report is an instance of class dict
student_report = {"John": 80, "Eric": 70, "Donald": 90}
print(isinstance(student_report, dict))
# Output True

# Check if names is an instance of class tuple
names = ("Sam", "Kelly", 'Emma')
print(isinstance(names, tuple))
# Output True

# Check if numbers is an instance of class tuple
numbers = {11, 22, 33, 44, 55}
print(isinstance(numbers, set))
# Output True
7 được nêu ra.
: If the
num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
8 argument is not a Class, type, or tuple of types, a
# Check if 80 is an instance of class int
number = 80
print(isinstance(number, int))
# output True

print(isinstance(number, float))
# output False

pi = 3.14
# Check 3.14 is an instance of class float
print(isinstance(pi, float))
# Output True

# Check if (1 + 2j) is an instance of complex
complex_num = 1 + 2j
print(isinstance(complex_num, complex))
# Output True

# Check if 'PYnative' is an instance of class string
name = "PYnative.com"
print(isinstance(name, str))
# Output True

# Check if names is an instance of class list
names = ["Eric", "Scott", "Kelly"]
print(isinstance(names, list))
# Output True

# Check if student_report is an instance of class dict
student_report = {"John": 80, "Eric": 70, "Donald": 90}
print(isinstance(student_report, dict))
# Output True

# Check if names is an instance of class tuple
names = ("Sam", "Kelly", 'Emma')
print(isinstance(names, tuple))
# Output True

# Check if numbers is an instance of class tuple
numbers = {11, 22, 33, 44, 55}
print(isinstance(numbers, set))
# Output True
7 exception is raised.

isinstance () với các loại tích hợp

Như bạn đã biết, mọi giá trị (biến) trong Python có một loại. Trong Python, chúng ta có thể sử dụng các loại tích hợp khác nhau như

Yes
5,
# Check if 80 is an instance of class int
number = 80
print(isinstance(number, int))
# output True

print(isinstance(number, float))
# output False

pi = 3.14
# Check 3.14 is an instance of class float
print(isinstance(pi, float))
# Output True

# Check if (1 + 2j) is an instance of complex
complex_num = 1 + 2j
print(isinstance(complex_num, complex))
# Output True

# Check if 'PYnative' is an instance of class string
name = "PYnative.com"
print(isinstance(name, str))
# Output True

# Check if names is an instance of class list
names = ["Eric", "Scott", "Kelly"]
print(isinstance(names, list))
# Output True

# Check if student_report is an instance of class dict
student_report = {"John": 80, "Eric": 70, "Donald": 90}
print(isinstance(student_report, dict))
# Output True

# Check if names is an instance of class tuple
names = ("Sam", "Kelly", 'Emma')
print(isinstance(names, tuple))
# Output True

# Check if numbers is an instance of class tuple
numbers = {11, 22, 33, 44, 55}
print(isinstance(numbers, set))
# Output True
9, danh sách, tuple, chuỗi, từ điển. Hầu hết thời gian, bạn muốn kiểm tra loại giá trị để thực hiện một số hoạt động. Trong trường hợp này, chức năng
num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
2 là hữu ích.

# Check if 80 is an instance of class int
number = 80
print(isinstance(number, int))
# output True

print(isinstance(number, float))
# output False

pi = 3.14
# Check 3.14 is an instance of class float
print(isinstance(pi, float))
# Output True

# Check if (1 + 2j) is an instance of complex
complex_num = 1 + 2j
print(isinstance(complex_num, complex))
# Output True

# Check if 'PYnative' is an instance of class string
name = "PYnative.com"
print(isinstance(name, str))
# Output True

# Check if names is an instance of class list
names = ["Eric", "Scott", "Kelly"]
print(isinstance(names, list))
# Output True

# Check if student_report is an instance of class dict
student_report = {"John": 80, "Eric": 70, "Donald": 90}
print(isinstance(student_report, dict))
# Output True

# Check if names is an instance of class tuple
names = ("Sam", "Kelly", 'Emma')
print(isinstance(names, tuple))
# Output True

# Check if numbers is an instance of class tuple
numbers = {11, 22, 33, 44, 55}
print(isinstance(numbers, set))
# Output True

Lưu ý: Nếu chúng tôi sử dụng

num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
2 với bất kỳ biến hoặc đối tượng nào có
var = None
# empty but not None
s1 = ''
print(isinstance(var, float))
# Output False
print(isinstance(s1, str))
# Output True
2, nó sẽ trả về
var = None
# empty but not None
s1 = ''
print(isinstance(var, float))
# Output False
print(isinstance(s1, str))
# Output True
3. Hãy xem ví dụ đơn giản của nó.
: If we use the
num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
2 with any variable or object with a
var = None
# empty but not None
s1 = ''
print(isinstance(var, float))
# Output False
print(isinstance(s1, str))
# Output True
2, it returns
var = None
# empty but not None
s1 = ''
print(isinstance(var, float))
# Output False
print(isinstance(s1, str))
# Output True
3. Let see the simple example of it.

var = None
# empty but not None
s1 = ''
print(isinstance(var, float))
# Output False
print(isinstance(s1, str))
# Output True

isinstance () với nhiều lớp

Bạn cũng có thể kiểm tra phiên bản với nhiều loại. Hãy nói rằng bạn có một biến và bạn muốn kiểm tra xem nó có giữ bất kỳ giá trị số nào hay không, ví dụ, giá trị số có thể là một

Yes
5 hoặc
# Check if 80 is an instance of class int
number = 80
print(isinstance(number, int))
# output True

print(isinstance(number, float))
# output False

pi = 3.14
# Check 3.14 is an instance of class float
print(isinstance(pi, float))
# Output True

# Check if (1 + 2j) is an instance of complex
complex_num = 1 + 2j
print(isinstance(complex_num, complex))
# Output True

# Check if 'PYnative' is an instance of class string
name = "PYnative.com"
print(isinstance(name, str))
# Output True

# Check if names is an instance of class list
names = ["Eric", "Scott", "Kelly"]
print(isinstance(names, list))
# Output True

# Check if student_report is an instance of class dict
student_report = {"John": 80, "Eric": 70, "Donald": 90}
print(isinstance(student_report, dict))
# Output True

# Check if names is an instance of class tuple
names = ("Sam", "Kelly", 'Emma')
print(isinstance(names, tuple))
# Output True

# Check if numbers is an instance of class tuple
numbers = {11, 22, 33, 44, 55}
print(isinstance(numbers, set))
# Output True
9.

Để xác minh xem một biến có phải là một thể hiện của một trong các loại được chỉ định hay không, chúng ta cần đề cập đến tất cả các loại trong một tuple và chuyển nó đến đối số classinfo của

num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
2.

Thí dụ

def check_number(var):
    if isinstance(var, (int, float)):
        print('variable', var, 'is instance of numeric type')
    else:
        print('variable', var, 'is not instance of numeric type')

num1 = 80
check_number(num1)
# Output variable 80 is instance of numeric type

num2 = 55.70
check_number(num2)
# Output variable 55.7 is instance of numeric type

num3 = '20'
check_number(num3)
# Output variable '20' is not instance of numeric type

Sử dụng isIntance (), chúng ta có thể xác minh xem một biến là số hoặc chuỗi. Hãy cùng giả sử biến

# Check if 80 is an instance of class int
number = 80
print(isinstance(number, int))
# output True

print(isinstance(number, float))
# output False

pi = 3.14
# Check 3.14 is an instance of class float
print(isinstance(pi, float))
# Output True

# Check if (1 + 2j) is an instance of complex
complex_num = 1 + 2j
print(isinstance(complex_num, complex))
# Output True

# Check if 'PYnative' is an instance of class string
name = "PYnative.com"
print(isinstance(name, str))
# Output True

# Check if names is an instance of class list
names = ["Eric", "Scott", "Kelly"]
print(isinstance(names, list))
# Output True

# Check if student_report is an instance of class dict
student_report = {"John": 80, "Eric": 70, "Donald": 90}
print(isinstance(student_report, dict))
# Output True

# Check if names is an instance of class tuple
names = ("Sam", "Kelly", 'Emma')
print(isinstance(names, tuple))
# Output True

# Check if numbers is an instance of class tuple
numbers = {11, 22, 33, 44, 55}
print(isinstance(numbers, set))
# Output True
2 và bạn muốn kiểm tra xem Num có phải là một thể hiện của loại INT hay không.

Như chúng ta có thể thấy trong đầu ra,

num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
2 đã trả lại
# Check if 80 is an instance of class int
number = 80
print(isinstance(number, int))
# output True

print(isinstance(number, float))
# output False

pi = 3.14
# Check 3.14 is an instance of class float
print(isinstance(pi, float))
# Output True

# Check if (1 + 2j) is an instance of complex
complex_num = 1 + 2j
print(isinstance(complex_num, complex))
# Output True

# Check if 'PYnative' is an instance of class string
name = "PYnative.com"
print(isinstance(name, str))
# Output True

# Check if names is an instance of class list
names = ["Eric", "Scott", "Kelly"]
print(isinstance(names, list))
# Output True

# Check if student_report is an instance of class dict
student_report = {"John": 80, "Eric": 70, "Donald": 90}
print(isinstance(student_report, dict))
# Output True

# Check if names is an instance of class tuple
names = ("Sam", "Kelly", 'Emma')
print(isinstance(names, tuple))
# Output True

# Check if numbers is an instance of class tuple
numbers = {11, 22, 33, 44, 55}
print(isinstance(numbers, set))
# Output True
4 vì
# Check if 80 is an instance of class int
number = 80
print(isinstance(number, int))
# output True

print(isinstance(number, float))
# output False

pi = 3.14
# Check 3.14 is an instance of class float
print(isinstance(pi, float))
# Output True

# Check if (1 + 2j) is an instance of complex
complex_num = 1 + 2j
print(isinstance(complex_num, complex))
# Output True

# Check if 'PYnative' is an instance of class string
name = "PYnative.com"
print(isinstance(name, str))
# Output True

# Check if names is an instance of class list
names = ["Eric", "Scott", "Kelly"]
print(isinstance(names, list))
# Output True

# Check if student_report is an instance of class dict
student_report = {"John": 80, "Eric": 70, "Donald": 90}
print(isinstance(student_report, dict))
# Output True

# Check if names is an instance of class tuple
names = ("Sam", "Kelly", 'Emma')
print(isinstance(names, tuple))
# Output True

# Check if numbers is an instance of class tuple
numbers = {11, 22, 33, 44, 55}
print(isinstance(numbers, set))
# Output True
5 giữ giá trị số nguyên.

class Employee:

    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

class Person:

    def __init__(self, name, sex):
        self.name = name
        self.sex = sex

emp = Employee("Emma", 11000)
per = Person("Brent", "male")

# Checking if a emp object is an instance of Employee
print(isinstance(emp, Employee))
# Output True

# Checking if the per object is an instance of Employee
print(isinstance(per, Employee))
# Output False

Lưu ý: Nếu đối số num = 90 result = isinstance(num, int) if result: print("Yes") else: print("No")8 không phải là một loại, loại hoặc tuple loại, thì ngoại lệ # Check if 80 is an instance of class int number = 80 print(isinstance(number, int)) # output True print(isinstance(number, float)) # output False pi = 3.14 # Check 3.14 is an instance of class float print(isinstance(pi, float)) # Output True # Check if (1 + 2j) is an instance of complex complex_num = 1 + 2j print(isinstance(complex_num, complex)) # Output True # Check if 'PYnative' is an instance of class string name = "PYnative.com" print(isinstance(name, str)) # Output True # Check if names is an instance of class list names = ["Eric", "Scott", "Kelly"] print(isinstance(names, list)) # Output True # Check if student_report is an instance of class dict student_report = {"John": 80, "Eric": 70, "Donald": 90} print(isinstance(student_report, dict)) # Output True # Check if names is an instance of class tuple names = ("Sam", "Kelly", 'Emma') print(isinstance(names, tuple)) # Output True # Check if numbers is an instance of class tuple numbers = {11, 22, 33, 44, 55} print(isinstance(numbers, set)) # Output True7 được nêu ra.

isinstance () với các loại tích hợp

Như bạn đã biết, mọi giá trị (biến) trong Python có một loại. Trong Python, chúng ta có thể sử dụng các loại tích hợp khác nhau như

Yes
5,
# Check if 80 is an instance of class int
number = 80
print(isinstance(number, int))
# output True

print(isinstance(number, float))
# output False

pi = 3.14
# Check 3.14 is an instance of class float
print(isinstance(pi, float))
# Output True

# Check if (1 + 2j) is an instance of complex
complex_num = 1 + 2j
print(isinstance(complex_num, complex))
# Output True

# Check if 'PYnative' is an instance of class string
name = "PYnative.com"
print(isinstance(name, str))
# Output True

# Check if names is an instance of class list
names = ["Eric", "Scott", "Kelly"]
print(isinstance(names, list))
# Output True

# Check if student_report is an instance of class dict
student_report = {"John": 80, "Eric": 70, "Donald": 90}
print(isinstance(student_report, dict))
# Output True

# Check if names is an instance of class tuple
names = ("Sam", "Kelly", 'Emma')
print(isinstance(names, tuple))
# Output True

# Check if numbers is an instance of class tuple
numbers = {11, 22, 33, 44, 55}
print(isinstance(numbers, set))
# Output True
9, danh sách, tuple, chuỗi, từ điển. Hầu hết thời gian, bạn muốn kiểm tra loại giá trị để thực hiện một số hoạt động. Trong trường hợp này, chức năng
num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
2 là hữu ích.

Lưu ý: Nếu chúng tôi sử dụng

num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
2 với bất kỳ biến hoặc đối tượng nào có
var = None
# empty but not None
s1 = ''
print(isinstance(var, float))
# Output False
print(isinstance(s1, str))
# Output True
2, nó sẽ trả về
var = None
# empty but not None
s1 = ''
print(isinstance(var, float))
# Output False
print(isinstance(s1, str))
# Output True
3. Hãy xem ví dụ đơn giản của nó.

isinstance () với nhiều lớp

class Developer(object):

    # Constructor
    def __init__(self, name):
        self.name = name

    def display(self):
        print("Developer:", self.name, "-")

class PythonDeveloper(Developer):

    # Constructor
    def __init__(self, name, language):
        self.name = name
        self.language = language

    def display(self):
        print("Python Developer:", self.name, "language:", self.language, "-")

# Object of PythonDeveloper
dev = PythonDeveloper("Eric", "Python")
# is PythonDeveloper object an instance of a PythonDeveloper Class
print(isinstance(dev, PythonDeveloper))
# Output True

# is python_dev object an instance of a Developer Class
print(isinstance(dev, Developer))
# Output True

Bạn cũng có thể kiểm tra phiên bản với nhiều loại. Hãy nói rằng bạn có một biến và bạn muốn kiểm tra xem nó có giữ bất kỳ giá trị số nào hay không, ví dụ, giá trị số có thể là một

Yes
5 hoặc
# Check if 80 is an instance of class int
number = 80
print(isinstance(number, int))
# output True

print(isinstance(number, float))
# output False

pi = 3.14
# Check 3.14 is an instance of class float
print(isinstance(pi, float))
# Output True

# Check if (1 + 2j) is an instance of complex
complex_num = 1 + 2j
print(isinstance(complex_num, complex))
# Output True

# Check if 'PYnative' is an instance of class string
name = "PYnative.com"
print(isinstance(name, str))
# Output True

# Check if names is an instance of class list
names = ["Eric", "Scott", "Kelly"]
print(isinstance(names, list))
# Output True

# Check if student_report is an instance of class dict
student_report = {"John": 80, "Eric": 70, "Donald": 90}
print(isinstance(student_report, dict))
# Output True

# Check if names is an instance of class tuple
names = ("Sam", "Kelly", 'Emma')
print(isinstance(names, tuple))
# Output True

# Check if numbers is an instance of class tuple
numbers = {11, 22, 33, 44, 55}
print(isinstance(numbers, set))
# Output True
9.
: The
num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
2 function is beneficial for casting objects at runtime because once you get to know the given class is a subclass of a parent class, you can do casting appropriately if required.

Để xác minh xem một biến có phải là một thể hiện của một trong các loại được chỉ định hay không, chúng ta cần đề cập đến tất cả các loại trong một tuple và chuyển nó đến đối số classinfo của num = 90 result = isinstance(num, int) if result: print("Yes") else: print("No")2.

num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
2 hoạt động như một toán tử so sánh và nó so sánh đối tượng với loại lớp được chỉ định.

Bạn có thể xác minh xem đối tượng

var = None
# empty but not None
s1 = ''
print(isinstance(var, float))
# Output False
print(isinstance(s1, str))
# Output True
8 là một thể hiện của một nhân viên lớp do người dùng xác định bằng hàm
num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
2. Nó phải trả về đúng.

  • chức năng isinstance () với sự kế thừa
  • Đối tượng của loại lớp con cũng là một loại lớp cha. Ví dụ, nếu xe là một lớp con của một chiếc xe, thì đối tượng của xe có thể được gọi bằng xe hơi hoặc xe. Trong trường hợp này,
    def check_number(var):
        if isinstance(var, (int, float)):
            print('variable', var, 'is instance of numeric type')
        else:
            print('variable', var, 'is not instance of numeric type')
    
    num1 = 80
    check_number(num1)
    # Output variable 80 is instance of numeric type
    
    num2 = 55.70
    check_number(num2)
    # Output variable 55.7 is instance of numeric type
    
    num3 = '20'
    check_number(num3)
    # Output variable '20' is not instance of numeric type
    
    0 sẽ trả lại
    # Check if 80 is an instance of class int
    number = 80
    print(isinstance(number, int))
    # output True
    
    print(isinstance(number, float))
    # output False
    
    pi = 3.14
    # Check 3.14 is an instance of class float
    print(isinstance(pi, float))
    # Output True
    
    # Check if (1 + 2j) is an instance of complex
    complex_num = 1 + 2j
    print(isinstance(complex_num, complex))
    # Output True
    
    # Check if 'PYnative' is an instance of class string
    name = "PYnative.com"
    print(isinstance(name, str))
    # Output True
    
    # Check if names is an instance of class list
    names = ["Eric", "Scott", "Kelly"]
    print(isinstance(names, list))
    # Output True
    
    # Check if student_report is an instance of class dict
    student_report = {"John": 80, "Eric": 70, "Donald": 90}
    print(isinstance(student_report, dict))
    # Output True
    
    # Check if names is an instance of class tuple
    names = ("Sam", "Kelly", 'Emma')
    print(isinstance(names, tuple))
    # Output True
    
    # Check if numbers is an instance of class tuple
    numbers = {11, 22, 33, 44, 55}
    print(isinstance(numbers, set))
    # Output True
    4.
  • Hàm
    num = 90
    result = isinstance(num, int)
    if result:
        print("Yes")
    else:
        print("No")
    2 hoạt động theo nguyên tắc của mối quan hệ IS-A. Khái niệm về mối quan hệ IS-A dựa trên kế thừa lớp học.
  • def check_number(var):
        if isinstance(var, (int, float)):
            print('variable', var, 'is instance of numeric type')
        else:
            print('variable', var, 'is not instance of numeric type')
    
    num1 = 80
    check_number(num1)
    # Output variable 80 is instance of numeric type
    
    num2 = 55.70
    check_number(num2)
    # Output variable 55.7 is instance of numeric type
    
    num3 = '20'
    check_number(num3)
    # Output variable '20' is not instance of numeric type
    
    3 trả về đúng nếu đối số classinfo của trường hợp () là lớp cha mẹ của lớp đối tượng.

Để chứng minh điều này, tôi đã tạo ra hai lớp, nhà phát triển và Pythondeveoper. Ở đây Pythondeveoper là một lớp phụ của một lớp nhà phát triển.

sample_list = ["Emma", "Stevan", "Brent"]
res = isinstance(sample_list, list)
print(sample_list, 'is instance of list?', res)

# Output 'Emma', 'Stevan', 'Brent'] is instance of list? True

Kiểm tra xem một phần tử của danh sách là danh sách lồng nhau

Để kiểm tra xem một trong các yếu tố trong danh sách có phải là một danh sách không. Ví dụ: bạn có danh sách sau, sử dụng

num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
2 để xác minh xem danh sách có chứa danh sách lồng nhau

sampleList = ['Emma', 'Stevan', ['Jordan', 'Donald', 'Sam']]

Lặp lại một danh sách và xác minh từng lớp phần tử và nếu nó là loại danh sách, chúng ta có thể nói rằng danh sách chứa một danh sách lồng nhau.

num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
0

Kiểm tra xem các yếu tố của danh sách là số hoặc chuỗi

Kiểm tra từng loại phần tử với nhiều loại số như

Yes
5,
# Check if 80 is an instance of class int
number = 80
print(isinstance(number, int))
# output True

print(isinstance(number, float))
# output False

pi = 3.14
# Check 3.14 is an instance of class float
print(isinstance(pi, float))
# Output True

# Check if (1 + 2j) is an instance of complex
complex_num = 1 + 2j
print(isinstance(complex_num, complex))
# Output True

# Check if 'PYnative' is an instance of class string
name = "PYnative.com"
print(isinstance(name, str))
# Output True

# Check if names is an instance of class list
names = ["Eric", "Scott", "Kelly"]
print(isinstance(names, list))
# Output True

# Check if student_report is an instance of class dict
student_report = {"John": 80, "Eric": 70, "Donald": 90}
print(isinstance(student_report, dict))
# Output True

# Check if names is an instance of class tuple
names = ("Sam", "Kelly", 'Emma')
print(isinstance(names, tuple))
# Output True

# Check if numbers is an instance of class tuple
numbers = {11, 22, 33, 44, 55}
print(isinstance(numbers, set))
# Output True
9 và
class Employee:

    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

class Person:

    def __init__(self, name, sex):
        self.name = name
        self.sex = sex

emp = Employee("Emma", 11000)
per = Person("Brent", "male")

# Checking if a emp object is an instance of Employee
print(isinstance(emp, Employee))
# Output True

# Checking if the per object is an instance of Employee
print(isinstance(per, Employee))
# Output False
0 sử dụng hàm
num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
2.

Để tìm tất cả các biến chuỗi, hãy kiểm tra từng loại phần tử với loại

num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
5.

num = 90
result = isinstance(num, int)
if result:
    print("Yes")
else:
    print("No")
1

Bước tiếp theo

Hãy cho tôi biết ý kiến ​​và phản hồi của bạn trong phần dưới đây.

Solve::

  • Bài tập Python cho người mới bắt đầu
  • Câu đố Python cho người mới bắt đầu

Là ví dụ của đối tượng Python?

Tất cả mọi thứ trong Python là một đối tượng như số nguyên, danh sách, từ điển, chức năng, v.v. Mỗi đối tượng có một loại và các loại đối tượng được tạo bằng các lớp. Ví dụ là một đối tượng thuộc về một lớp. Ví dụ, danh sách là một lớp học trong Python.Instance is an object that belongs to a class. For instance, list is a class in Python.

Làm cách nào để kiểm tra xem một đối tượng là một thể hiện của một lớp nhất định hoặc của một lớp con của nó?

Cú pháp của hàm isInStance () Trả về: Đúng nếu đối tượng là một thể hiện hoặc phân lớp của một lớp hoặc bất kỳ yếu tố nào của tple false khác. Nếu thông tin lớp không phải là một loại hoặc tuple loại, ngoại lệ kiểu loại sẽ được nâng lên. Ví dụ 1: Trong ví dụ này, chúng ta sẽ thấy Test isInstance () cho đối tượng lớp.isinstance() function Return: true if the object is an instance or subclass of a class, or any element of the tuple false otherwise. If class info is not a type or tuple of types, a TypeError exception is raised. Example 1: In this example, we will see test isinstance() for the class object.

Có phải mọi đối tượng là một ví dụ của một python lớp?

Mỗi phương thức có một tên, được gọi là bộ chọn của nó, là duy nhất trong lớp.Vì các lớp là đối tượng và mọi đối tượng là một thể hiện của một lớp, nên theo các lớp cũng phải là trường hợp của các lớp.every object is an instance of a class, it follows that classes must also be instances of classes.

Làm thế nào để bạn kiểm tra xem một cái gì đó là một đối tượng trong Python?

Trong Python, loại chức năng tích hợp () và isinstance () giúp bạn xác định loại đối tượng.Loại (Đối tượng) - Trả về một biểu diễn chuỗi của loại đối tượng.isinstance (Đối tượng, lớp) - Trả về một sự thật Boolean nếu đối tượng là một thể hiện của lớp và sai.