Lớp dữ liệu Python, trường

Để tham khảo, một lớp về cơ bản là một kế hoạch chi tiết để tạo các đối tượng. Một ví dụ về một lớp có thể là một quốc gia, mà chúng ta sẽ sử dụng lớp

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
3 để tạo các thể hiện khác nhau, chẳng hạn như Monaco và Gambia

Khi khởi tạo giá trị, các thuộc tính được cung cấp cho hàm tạo [như dân số, ngôn ngữ, v.v.] được sao chép vào từng thể hiện đối tượng

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang = official_lang


smallestEurope = Country["Monaco", 37623, "Europe"]
smallestAsia= Country["Maldives", 552595, "Asia"]
smallestAfrica= Country["Gambia", 2521126, "Africa"] 

Nếu bạn đã từng làm việc với lập trình hướng đối tượng [OOP] bằng các ngôn ngữ lập trình như Java và Python, thì bạn hẳn đã quen thuộc với các lớp.

Tuy nhiên, một

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
4 đi kèm với các chức năng lớp cơ bản đã được triển khai, giúp giảm thời gian viết mã

Trong bài viết này, chúng ta sẽ tìm hiểu sâu hơn về

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
1 trong Python là gì, cách thao tác với các trường đối tượng, cách sắp xếp và so sánh
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
1, v.v.

Lưu ý rằng vì điều này đã được phát hành trong Python 3. 7, bạn phải cài đặt phiên bản Python gần đây trên máy cục bộ của mình để sử dụng nó

Python
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
4 là gì?

Như đã đề cập trước đây, Python

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
1 rất giống với các lớp thông thường, nhưng với các chức năng của lớp được triển khai giúp giảm đáng kể số lượng mã soạn sẵn cần thiết để viết

Một ví dụ về bản soạn sẵn như vậy là phương pháp

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
9

Trong ví dụ về lớp

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
3, bạn có thể quan sát thấy rằng chúng ta phải định nghĩa phương thức
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
9 theo cách thủ công, phương thức này được gọi khi bạn khởi tạo lớp. Bây giờ, đối với mọi lớp bình thường mà bạn định nghĩa, bạn được yêu cầu cung cấp chức năng này, điều đó có nghĩa là bạn phải viết rất nhiều mã lặp đi lặp lại

Python

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
4 đi kèm với phương thức này đã được xác định. Vì vậy, bạn có thể viết cùng một lớp
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
3 mà không cần xác định hàm tạo theo cách thủ công

Tóm lại,

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang = official_lang


smallestEurope = Country["Monaco", 37623, "Europe"]
smallestAsia= Country["Maldives", 552595, "Asia"]
smallestAfrica= Country["Gambia", 2521126, "Africa"] 
24 gọi phương thức này khi bạn khởi tạo đối tượng với các thuộc tính mới

Hơn 200 nghìn nhà phát triển sử dụng LogRocket để tạo ra trải nghiệm kỹ thuật số tốt hơn

Tìm hiểu thêm →

Lưu ý rằng

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
9 không phải là phương thức duy nhất được cung cấp theo mặc định. Các phương thức tiện ích khác như
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang = official_lang


smallestEurope = Country["Monaco", 37623, "Europe"]
smallestAsia= Country["Maldives", 552595, "Asia"]
smallestAfrica= Country["Gambia", 2521126, "Africa"] 
26 [đại diện],
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang = official_lang


smallestEurope = Country["Monaco", 37623, "Europe"]
smallestAsia= Country["Maldives", 552595, "Asia"]
smallestAfrica= Country["Gambia", 2521126, "Africa"] 
27 [nhỏ hơn],
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang = official_lang


smallestEurope = Country["Monaco", 37623, "Europe"]
smallestAsia= Country["Maldives", 552595, "Asia"]
smallestAfrica= Country["Gambia", 2521126, "Africa"] 
28 [lớn hơn],
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang = official_lang


smallestEurope = Country["Monaco", 37623, "Europe"]
smallestAsia= Country["Maldives", 552595, "Asia"]
smallestAfrica= Country["Gambia", 2521126, "Africa"] 
29 [bằng] và nhiều phương thức khác cũng được triển khai theo mặc định

Sử dụng lớp Python bình thường

Khi làm việc với một lớp bình thường trong Python, chúng ta có mã dài hơn để triển khai các phương thức cơ bản

Hãy xem xét lớp

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
3 một lần nữa. Trong khối mã bên dưới, bạn có thể thấy một số phương thức, bắt đầu bằng phương thức
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang = official_lang


smallestEurope = Country["Monaco", 37623, "Europe"]
smallestAsia= Country["Maldives", 552595, "Asia"]
smallestAfrica= Country["Gambia", 2521126, "Africa"] 
91. Phương thức này khởi tạo các thuộc tính như tên quốc gia, số lượng dân số, lục địa và ngôn ngữ chính thức trên đối tượng
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
3

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang = official_lang


smallestEurope = Country["Monaco", 37623, "Europe"]
smallestAsia= Country["Maldives", 552595, "Asia"]
smallestAfrica= Country["Gambia", 2521126, "Africa"] 
26 trả về biểu diễn chuỗi của một thể hiện lớp. Điều này in các thuộc tính của từng cá thể lớp ở dạng chuỗi

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang = official_lang


smallestEurope = Country["Monaco", 37623, "Europe"]
smallestAsia= Country["Maldives", 552595, "Asia"]
smallestAfrica= Country["Gambia", 2521126, "Africa"] 
94 so sánh dân số của hai trường hợp
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
3 và trả về
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang = official_lang


smallestEurope = Country["Monaco", 37623, "Europe"]
smallestAsia= Country["Maldives", 552595, "Asia"]
smallestAfrica= Country["Gambia", 2521126, "Africa"] 
96 nếu trường hợp hiện tại có dân số ít hơn, trong khi
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang = official_lang


smallestEurope = Country["Monaco", 37623, "Europe"]
smallestAsia= Country["Maldives", 552595, "Asia"]
smallestAfrica= Country["Gambia", 2521126, "Africa"] 
97 trả về
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang = official_lang


smallestEurope = Country["Monaco", 37623, "Europe"]
smallestAsia= Country["Maldives", 552595, "Asia"]
smallestAfrica= Country["Gambia", 2521126, "Africa"] 
96 nếu cả hai có cùng số lượng dân số

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False

Sử dụng Python
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
4

Để sử dụng

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
4 của Python trong mã của bạn, chỉ cần nhập mô-đun và đăng ký trình trang trí
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang = official_lang


smallestEurope = Country["Monaco", 37623, "Europe"]
smallestAsia= Country["Maldives", 552595, "Asia"]
smallestAfrica= Country["Gambia", 2521126, "Africa"] 
24 ở đầu lớp. Điều này sẽ tự động đưa các chức năng của lớp cơ sở vào lớp của chúng ta

Trong ví dụ sau, chúng tôi sẽ tạo cùng một lớp

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
3, nhưng với ít mã hơn

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang = official_lang


smallestEurope = Country["Monaco", 37623, "Europe"]
smallestAsia= Country["Maldives", 552595, "Asia"]
smallestAfrica= Country["Gambia", 2521126, "Africa"] 
2

Lưu ý rằng chúng tôi đã không xác định một phương thức xây dựng trên

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
4;

Chúng tôi cũng bỏ qua những người trợ giúp như

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
44 và
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang = official_lang


smallestEurope = Country["Monaco", 37623, "Europe"]
smallestAsia= Country["Maldives", 552595, "Asia"]
smallestAfrica= Country["Gambia", 2521126, "Africa"] 
29. Dù lược bỏ các phương thức này nhưng lớp vẫn chạy bình thường

Các bài viết hay khác từ LogRocket

  • Đừng bỏ lỡ một khoảnh khắc nào với The Replay, một bản tin được tuyển chọn từ LogRocket
  • Tìm hiểu cách Galileo của LogRocket loại bỏ tiếng ồn để chủ động giải quyết các sự cố trong ứng dụng của bạn
  • Sử dụng useEffect của React để tối ưu hóa hiệu suất ứng dụng của bạn
  • Chuyển đổi giữa nhiều phiên bản của Node
  • Khám phá cách tạo hoạt ảnh cho ứng dụng React của bạn với AnimXYZ
  • Khám phá Tauri, một khuôn khổ mới để xây dựng các tệp nhị phân
  • So sánh NestJS với. Thể hiện. js

Lưu ý rằng đối với ít hơn [

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
46],
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
4 sử dụng phương thức mặc định để so sánh các đối tượng. Ở phần sau của bài viết này, chúng ta sẽ tìm hiểu cách tùy chỉnh so sánh đối tượng để có kết quả tốt hơn

Thao tác với các trường đối tượng bằng hàm
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
48

Mô-đun

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
4 cũng cung cấp một chức năng có tên là
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
48. Chức năng này cung cấp cho bạn quyền kiểm soát cố hữu đối với các trường lớp, cho phép bạn thao tác và tùy chỉnh chúng theo ý muốn

Ví dụ: chúng ta có thể loại trừ trường

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
11 khi gọi phương thức biểu diễn bằng cách chuyển cho nó tham số
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
44 và đặt giá trị thành
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
13

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang = official_lang


smallestEurope = Country["Monaco", 37623, "Europe"]
smallestAsia= Country["Maldives", 552595, "Asia"]
smallestAfrica= Country["Gambia", 2521126, "Africa"] 
9

Mã này sau đó xuất ra trong CLI

Theo mặc định,

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
44 luôn được đặt thành
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang = official_lang


smallestEurope = Country["Monaco", 37623, "Europe"]
smallestAsia= Country["Maldives", 552595, "Asia"]
smallestAfrica= Country["Gambia", 2521126, "Africa"] 
96

Dưới đây là một số tham số khác có thể được đưa vào bởi

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
48

tham số
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
17

Tham số

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
17 chuyển để chỉ định xem một thuộc tính có nên được đưa vào làm đối số cho hàm tạo trong quá trình khởi tạo hay không. Nếu bạn đặt trường thành
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
19, thì bạn phải bỏ qua thuộc tính trong quá trình khởi tạo. Nếu không, một
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
10 sẽ bị ném

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
4

Mã này sau đó xuất ra trong CLI

Tham số
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
11

Tham số

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
11 được truyền để chỉ định giá trị mặc định cho một trường trong trường hợp giá trị không được cung cấp trong quá trình khởi tạo

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
1

Mã này sau đó xuất ra trong CLI

tham số
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
44

Tham số

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
44 chuyển để chỉ định xem nên bao gồm trường [
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
15] hay loại trừ [
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
16] khỏi biểu diễn chuỗi, như được tạo bởi phương thức
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang = official_lang


smallestEurope = Country["Monaco", 37623, "Europe"]
smallestAsia= Country["Maldives", 552595, "Asia"]
smallestAfrica= Country["Gambia", 2521126, "Africa"] 
26

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
1

Mã này sau đó xuất ra trong CLI

Sửa đổi các trường sau khi khởi tạo với
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
18

Phương thức

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
18 được gọi ngay sau khi khởi tạo. Nói cách khác, nó được gọi sau khi đối tượng nhận các giá trị cho các trường của nó, chẳng hạn như
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
40,
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
11,
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
42 và
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
43

Ví dụ: chúng tôi sẽ sử dụng phương pháp để xác định xem chúng tôi có định di cư đến một quốc gia hay không, dựa trên ngôn ngữ chính thức của quốc gia đó

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
4

Sau khi đối tượng khởi tạo với các giá trị, chúng tôi thực hiện kiểm tra xem trường

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
43 có được đặt thành
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
45 từ bên trong
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
46 không. Nếu vậy, chúng ta phải đặt thuộc tính
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
47 thành
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
48. Mặt khác, chúng tôi đặt nó thành
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
13

Sắp xếp và so sánh
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
1 với
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
61

Một chức năng khác của

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
1 là khả năng tạo thứ tự tùy chỉnh để so sánh các đối tượng và sắp xếp danh sách các đối tượng

Ví dụ: chúng ta có thể so sánh hai quốc gia theo số dân của họ. Nói cách khác, chúng tôi muốn nói rằng một quốc gia lớn hơn một quốc gia khác khi và chỉ khi dân số của quốc gia đó lớn hơn quốc gia kia

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
6

Để cho phép so sánh và sắp xếp trong Python

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
4, bạn phải chuyển thuộc tính
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
64 cho
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang = official_lang


smallestEurope = Country["Monaco", 37623, "Europe"]
smallestAsia= Country["Maldives", 552595, "Asia"]
smallestAfrica= Country["Gambia", 2521126, "Africa"] 
24 với giá trị
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
48. Điều này cho phép chức năng so sánh mặc định

Vì chúng tôi muốn so sánh theo số lượng dân số, chúng tôi phải chuyển trường

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
42 cho thuộc tính
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
61 sau khi khởi tạo từ bên trong phương thức
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
69

Bạn cũng có thể sắp xếp danh sách các đối tượng bằng cách sử dụng một trường cụ thể dưới dạng

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
61. Ví dụ: chúng ta phải sắp xếp danh sách các quốc gia theo số lượng dân số của họ

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
7

Mã này sau đó xuất ra trong CLI

Bạn không muốn

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
4 bị giả mạo?

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
0

kết thúc

Python

class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
4 là một tính năng rất mạnh giúp giảm đáng kể lượng mã trong các định nghĩa lớp. Mô-đun cung cấp hầu hết các phương thức lớp cơ bản đã được triển khai. Bạn có thể tùy chỉnh các trường trong
class Country:
    def __init__[self, name: str, population: int, continent: str, official_lang: str="English" ]:
        self.name = name
        self.population = population
        self.continent = continent
        self.official_lang= official_lang

   def __repr__[self]:
        return[f"Country[name={self.name},
            population={self.population}, continent={self.continent},
            official_lang={self.official_lang}]"]

   def __lt__[self, other]:
        return self.population < other.population

   def __eq__[self, other]:
        return self.population == other.population


smallestAfrica= Country["Gambia", 2521126, "Africa", "English"]
smallestEurope = Country["Monaco", 37623, "Europe", "French"]
smallestAsia1= Country["Maldives", 552595, "Asia", "Dhivehi"]
smallestAsia2= Country["Maldives", 552595, "Asia", "Dhivehi"]


print[smallestAfrica] 
# Country[name='Gambia', population=2521126, continent='Africa', #official_lang='English']

print[smallestAsia < smallestAfrica] # True
print[smallestAsia > smallestAfrica] # False
4 và hạn chế một số hành động nhất định

Đăng NhậpTên Lửa. Khả năng hiển thị đầy đủ vào web và ứng dụng dành cho thiết bị di động của bạn

LogRocket là một giải pháp giám sát ứng dụng giao diện người dùng cho phép bạn phát lại các sự cố như thể chúng đã xảy ra trong trình duyệt của chính bạn. Thay vì đoán tại sao xảy ra lỗi hoặc yêu cầu người dùng chụp ảnh màn hình và kết xuất nhật ký, LogRocket cho phép bạn phát lại phiên để nhanh chóng hiểu điều gì đã xảy ra. Nó hoạt động hoàn hảo với bất kỳ ứng dụng nào, bất kể khung công tác nào và có các plugin để ghi lại ngữ cảnh bổ sung từ Redux, Vuex và @ngrx/store

Ngoài việc ghi nhật ký các hành động và trạng thái Redux, LogRocket còn ghi nhật ký bảng điều khiển, lỗi JavaScript, dấu vết ngăn xếp, yêu cầu/phản hồi mạng với tiêu đề + nội dung, siêu dữ liệu trình duyệt và nhật ký tùy chỉnh. Nó cũng cung cấp công cụ cho DOM để ghi lại HTML và CSS trên trang, tạo lại các video hoàn hảo về pixel của ngay cả các ứng dụng dành cho thiết bị di động và trang đơn phức tạp nhất

Trường trong dataclass là gì?

Trình trang trí dataclass[] kiểm tra lớp để tìm trường s. Một trường được định nghĩa là biến lớp có chú thích kiểu .

@dataclass trong Python là gì?

Lớp dữ liệu là các lớp python, nhưng phù hợp để lưu trữ các đối tượng dữ liệu . Mô-đun này cung cấp một trình trang trí và các chức năng để tự động thêm các phương thức đặc biệt đã tạo như __init__[] và __repr__[] vào các lớp do người dùng định nghĩa.

Dataclass có thể có các phương thức Python không?

Hơn nữa, vì các lớp dữ liệu tương đối mới trong hệ sinh thái Python, nên nó thực thi các thực tiễn hiện đại như chú thích kiểu. 👉 lớp dữ liệu vẫn là lớp. Do đó, bạn có thể triển khai bất kỳ phương thức tùy chỉnh nào trong các lớp đó giống như bạn thực hiện trong một lớp thông thường .

Dataclass có thể kế thừa không?

Trong bài đăng này, chúng ta sẽ thảo luận về cách DataClass hoạt động khi được kế thừa. Mặc dù chúng tạo hàm tạo của riêng mình, nhưng Lớp dữ liệu hoạt động khá giống với cách các lớp thông thường thực hiện khi được kế thừa .

Chủ Đề