Lớp và kiểu có giống nhau trong Python không?

Trong bài viết này, chúng ta sẽ tìm hiểu về hàm type() và isinstance() trong Python, và sự khác nhau giữa type() và isinstance()

Loại trong Python là gì?

Python có một phương thức tích hợp được gọi là type thường có ích khi tìm ra loại biến được sử dụng trong chương trình trong thời gian chạy. Cách chính tắc để kiểm tra loại trong Python được đưa ra dưới đây

Cú pháp của hàm type()

type(object)
type(name, bases, dict)

ví dụ 1. Ví dụ về kiểu () với một tham số đối tượng đơn

Trong ví dụ này, chúng tôi đang cố gắng kiểm tra kiểu dữ liệu của từng biến, chẳng hạn như x, s và y bằng cách sử dụng hàm type()

Python3




# Python code type() with a single object parameter

class 'int'
class 'str'
class 'list'
0
class 'int'
class 'str'
class 'list'
1
class 'int'
class 'str'
class 'list'
2

class 'int'
class 'str'
class 'list'
3_______1_______1
class 'int'
class 'str'
class 'list'
5

class 'int'
class 'str'
class 'list'
6
class 'int'
class 'str'
class 'list'
1
class 'int'
class 'str'
class 'list'
8
class 'int'
class 'str'
class 'list'
9
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
0
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
1
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
0
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
3
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
4

{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
5
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
7
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
8

{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
5
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
7
True
False
True
2

{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
5
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
7
True
False
True
6

đầu ra

class 'int'
class 'str'
class 'list'

ví dụ 2. Ví dụ về kiểu () có tên, cơ sở và tham số dict

Nếu bạn cần kiểm tra loại đối tượng, bạn nên sử dụng hàm isinstance() của Python để thay thế. Đó là bởi vì hàm isinstance() cũng kiểm tra xem đối tượng đã cho có phải là một thể hiện của lớp con hay không

Python3




True
False
True
7

True
False
True
8

 

True
False
True
9_______1_______1
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
7
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
class 'int'
class 'str'
class 'list'
1
is a tuple: True
is a set: True
is a list: True
is a dict: True
0
is a tuple: True
is a set: True
is a list: True
is a dict: True
1
class 'int'
class 'str'
class 'list'
1
is a tuple: True
is a set: True
is a list: True
is a dict: True
3
is a tuple: True
is a set: True
is a list: True
is a dict: True
4

 

{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
5
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
7
is a tuple: True
is a set: True
is a list: True
is a dict: True
8

{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
5
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
Geeksforgeeks
Fox
Bear
1
is a tuple: True
is a set: True
is a list: True
is a dict: True
8

 

 

Geeksforgeeks
Fox
Bear
3
Geeksforgeeks
Fox
Bear
4

Geeksforgeeks
Fox
Bear
5
Geeksforgeeks
Fox
Bear
6
class 'int'
class 'str'
class 'list'
1
is a tuple: True
is a set: True
is a list: True
is a dict: True
0

 

 

Geeksforgeeks
Fox
Bear
9
class 'int'
class 'str'
class 'list'
1
is a tuple: True
is a set: True
is a list: True
is a dict: True
3

 

False
True
True
False
2_______1_______1
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
7
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6_______64_______6
False
True
True
False
7
is a float: True
is an integer: True
is a string: True
7
is a float: True
is an integer: True
is a string: True
8
class 'int'
class 'str'
class 'list'
1
is a tuple: True
is a set: True
is a list: True
is a dict: True
0
is a tuple: True
is a set: True
is a list: True
is a dict: True
1
class 'int'
class 'str'
class 'list'
1
is a tuple: True
is a set: True
is a list: True
is a dict: True
3
is a tuple: True
is a set: True
is a list: True
is a dict: True
4

{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
5
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
7
True
True
False
True
9

{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
5
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
Geeksforgeeks
Fox
Bear
1
True
True
False
True
9

đầu ra

{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}

Isinstance() trong Python là gì?

Hàm isinstance() kiểm tra xem đối tượng (đối số thứ nhất) là một thể hiện hay lớp con của lớp thông tin lớp (đối số thứ hai)

Cú pháp hàm isinstance()

cú pháp. isinstance(đối tượng, thông tin lớp)

Tham số

  • mục tiêu. đối tượng cần kiểm tra
  • thông tin lớp học. lớp, loại hoặc bộ của các lớp và loại

Trở lại. đúng nếu đối tượng là một thể hiện hoặc lớp con của một lớp hoặc bất kỳ phần tử nào của bộ dữ liệu sai.  

Nếu thông tin lớp không phải là một loại hoặc bộ loại, một ngoại lệ TypeError sẽ được đưa ra

ví dụ 1.  

Trong ví dụ này, chúng ta sẽ thấy test isinstance() cho đối tượng lớp

Python3




# Python code type() with a single object parameter4

Geeksforgeeks
Fox
Bear
3 # Python code type() with a single object parameter6

Geeksforgeeks
Fox
Bear
5
Geeksforgeeks
Fox
Bear
6
class 'int'
class 'str'
class 'list'
1
class 'int'
class 'str'
class 'list'
2

 

 

class 'int'
class 'str'
class 'list'
01______1_______1
class 'int'
class 'str'
class 'list'
03

 

{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
5
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
class 'int'
class 'str'
class 'list'
06
class 'int'
class 'str'
class 'list'
07

{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
5
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
class 'int'
class 'str'
class 'list'
06
class 'int'
class 'str'
class 'list'
11
class 'int'
class 'str'
class 'list'
12
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
0
class 'int'
class 'str'
class 'list'
14
class 'int'
class 'str'
class 'list'
15

{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
5
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
class 'int'
class 'str'
class 'list'
06
class 'int'
class 'str'
class 'list'
11
class 'int'
class 'str'
class 'list'
12
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
0
class 'int'
class 'str'
class 'list'
14
class 'int'
class 'str'
class 'list'
23

đầu ra

True
False
True

ví dụ 2

Trong ví dụ này, chúng ta sẽ thấy test isinstance() cho đối tượng integer, float và string

Python3




class 'int'
class 'str'
class 'list'
24
class 'int'
class 'str'
class 'list'
1
class 'int'
class 'str'
class 'list'
06
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6_______1_______28
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
0
class 'int'
class 'str'
class 'list'
30
class 'int'
class 'str'
class 'list'
31

{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
5
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
class 'int'
class 'str'
class 'list'
34
class 'int'
class 'str'
class 'list'
35

 

class 'int'
class 'str'
class 'list'
36_______1_______1
class 'int'
class 'str'
class 'list'
06
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
class 'int'
class 'str'
class 'list'
40
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
0
class 'int'
class 'str'
class 'list'
42
class 'int'
class 'str'
class 'list'
31

{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
5
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
class 'int'
class 'str'
class 'list'
46
class 'int'
class 'str'
class 'list'
47

 

 

class 'int'
class 'str'
class 'list'
48
class 'int'
class 'str'
class 'list'
1
class 'int'
class 'str'
class 'list'
06
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
class 'int'
class 'str'
class 'list'
52
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
0
class 'int'
class 'str'
class 'list'
54
class 'int'
class 'str'
class 'list'
31

{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
5
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
class 'int'
class 'str'
class 'list'
58
class 'int'
class 'str'
class 'list'
59

đầu ra

is a float: True
is an integer: True
is a string: True

ví dụ 3

Trong ví dụ này, chúng ta sẽ thấy test isinstance() cho tuple, list, dictionary và set object

Python3




class 'int'
class 'str'
class 'list'
60
class 'int'
class 'str'
class 'list'
1
class 'int'
class 'str'
class 'list'
06
class 'int'
class 'str'
class 'list'
63_______1_______64
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
0
class 'int'
class 'str'
class 'list'
66
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
0
class 'int'
class 'str'
class 'list'
68
class 'int'
class 'str'
class 'list'
69
class 'int'
class 'str'
class 'list'
14
class 'int'
class 'str'
class 'list'
31

{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
5
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
class 'int'
class 'str'
class 'list'
74
class 'int'
class 'str'
class 'list'
75

 

class 'int'
class 'str'
class 'list'
76
class 'int'
class 'str'
class 'list'
1
class 'int'
class 'str'
class 'list'
06
class 'int'
class 'str'
class 'list'
79_______1_______64
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
0
class 'int'
class 'str'
class 'list'
66
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
0
class 'int'
class 'str'
class 'list'
68
class 'int'
class 'str'
class 'list'
85
class 'int'
class 'str'
class 'list'
86
class 'int'
class 'str'
class 'list'
31

{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
5
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
class 'int'
class 'str'
class 'list'
90
class 'int'
class 'str'
class 'list'
91

 

class 'int'
class 'str'
class 'list'
92_______1_______1
class 'int'
class 'str'
class 'list'
06
class 'int'
class 'str'
class 'list'
95
class 'int'
class 'str'
class 'list'
64
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
0
class 'int'
class 'str'
class 'list'
66
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
0
class 'int'
class 'str'
class 'list'
68
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
01
class 'int'
class 'str'
class 'list'
12
class 'int'
class 'str'
class 'list'
31

{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
5
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
06
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
07

 

{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
08
class 'int'
class 'str'
class 'list'
1
class 'int'
class 'str'
class 'list'
06
class 'int'
class 'str'
class 'list'
79_______11_______12
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
13
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
14
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
0
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
16
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
13
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
18
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
0
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
20
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
13
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
22_______1_______85_______35_______1

{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
5
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
6
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
28
{'b': 12, 'a': 'Foo', '__dict__': , '__doc__': None, '__weakref__': }
{'b': 12, 'a': 'Foo', '__doc__': None}
29

đầu ra

is a tuple: True
is a set: True
is a list: True
is a dict: True

Sự khác biệt giữa loại () và isinstance () là gì?

Một lỗi cơ bản mà mọi người mắc phải là sử dụng hàm type() trong đó hàm isinstance() sẽ phù hợp hơn

Lớp có phải là một loại trong Python không?

Nói chung, loại của bất kỳ lớp kiểu mới nào là loại . loại là một siêu dữ liệu, trong đó các lớp là các thể hiện. Giống như một đối tượng thông thường là một thể hiện của một lớp, bất kỳ lớp kiểu mới nào trong Python và do đó, bất kỳ lớp nào trong Python 3, đều là một thể hiện của siêu dữ liệu kiểu.

Lớp và loại có giống nhau không?

Lớp định nghĩa trạng thái bên trong của đối tượng và việc thực hiện các hoạt động của nó. Ngược lại, loại của một đối tượng chỉ đề cập đến giao diện của nó - một tập hợp các yêu cầu mà nó có thể đáp ứng . Một đối tượng có thể có nhiều kiểu và các đối tượng thuộc các lớp khác nhau có thể có cùng kiểu.

Lớp và các loại của nó trong Python là gì?

Một lớp Python giống như bản phác thảo để tạo một đối tượng mới . Một đối tượng là bất cứ thứ gì bạn muốn thao tác hoặc thay đổi trong khi làm việc với mã. Mỗi khi một đối tượng lớp được khởi tạo, đó là khi chúng ta khai báo một biến, một đối tượng mới sẽ được khởi tạo từ đầu.

Kiểu () trong Python là gì?

Hàm type() được dùng để lấy loại đối tượng . Cú pháp hàm kiểu Python () là. loại (đối tượng) loại (tên, căn cứ, chính tả)