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.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
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ớpnum = 90 result = isinstance[num, int] if result: print["Yes"] else: print["No"]
8num = 90 result = isinstance[num, int] if result: print["Yes"] else: print["No"]
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.- Pass đối tượng đến isinstance []
Chuyển biến bạn muốn kiểm tra dưới dạng đối số
7 chonum = 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àonum = 90 result = isinstance[num, int] if result: print["Yes"] else: print["No"]
- Chỉ định tên loại hoặc loại là đối số
8num = 90 result = isinstance[num, int] if result: print["Yes"] else: print["No"]
Ví dụ:
Yes
3 để kiểm tra xemYes
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 quaYes
5,
5,num = 90 result = isinstance[num, int] if result: print["Yes"] else: print["No"]
Yes
9,
0 hoặc bất kỳ lớp do người dùng tạo.# 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
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.num = 90 result = isinstance[num, int] if result: print["Yes"] else: print["No"]
You can also pass multiple classes/types in a tuple format. For example, you can passYes
5,
5,num = 90 result = isinstance[num, int] if result: print["Yes"] else: print["No"]
Yes
9,
0, or any user-created class.# 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
- Thực hiện thao tác của bạn, nếu kết quả là đúng
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.num = 90 result = isinstance[num, int] if result: print["Yes"] else: print["No"]
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ố
8 không phải là một loại, loại hoặc tuple loại, thì ngoại lệ num = 90
result = isinstance[num, int]
if result:
print["Yes"]
else:
print["No"]
7 được nêu ra.: If the # 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
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ư
Yes5,
# 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
2 với bất kỳ biến hoặc đối tượng nào có num = 90
result = isinstance[num, int]
if result:
print["Yes"]
else:
print["No"]
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 var = None
# empty but not None
s1 = ''
print[isinstance[var, float]]
# Output False
print[isinstance[s1, str]]
# Output True
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
Yes5 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 True
7 được nêu ra.
num = 90
result = isinstance[num, int]
if result:
print["Yes"]
else:
print["No"]
# 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
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ư
Yes5,
# 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
9.: The # 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
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"]
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,
0 sẽ trả lạidef 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
4.# 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
- Hàm
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.num = 90 result = isinstance[num, int] if result: print["Yes"] else: print["No"]
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.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
Để 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 nhausampleList = ['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"]
0Kiể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ư
Yes5,
# 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"]
1Bướ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