Hướng dẫn python update attribute - thuộc tính cập nhật python

Nếu cập nhật một thuộc tính do bản cập nhật trên một thuộc tính khác là những gì bạn đang tìm kiếm (thay vì tính toán lại giá trị của thuộc tính hạ nguồn trên truy cập), hãy sử dụng các setters thuộc tính:

Nội phân chính

  • Các thuộc tính không tự động thay đổi
  • Chúng tôi không sử dụng các phương thức getter
  • Chúng tôi thích các thuộc tính hơn các phương thức getter
  • Làm thế nào để bạn thay đổi thuộc tính lớp trong Python?
  • Làm thế nào để bạn truy cập các thuộc tính của một lớp trong Python?
  • Thuộc tính trong lớp Python là gì?
  • Làm thế nào để bạn khởi tạo một thuộc tính lớp trong Python?

Nội phân chính

  • Các thuộc tính không tự động thay đổi
  • Chúng tôi không sử dụng các phương thức getter
  • Chúng tôi thích các thuộc tính hơn các phương thức getter
  • Làm thế nào để bạn thay đổi thuộc tính lớp trong Python?
  • Làm thế nào để bạn truy cập các thuộc tính của một lớp trong Python?
  • Thuộc tính trong lớp Python là gì?
  • Làm thế nào để bạn khởi tạo một thuộc tính lớp trong Python?

Nội phân chính

  • Các thuộc tính không tự động thay đổi
  • Chúng tôi không sử dụng các phương thức getter
  • Chúng tôi thích các thuộc tính hơn các phương thức getter
  • Làm thế nào để bạn thay đổi thuộc tính lớp trong Python?
  • Làm thế nào để bạn truy cập các thuộc tính của một lớp trong Python?
  • Thuộc tính trong lớp Python là gì?
  • Làm thế nào để bạn khởi tạo một thuộc tính lớp trong Python?
class SomeClass(object):
    def __init__(self, n):
        self.list = range(0, n)

    @property
    def list(self):
        return self._list
    @list.setter
    def list(self, val):
        self._list = val
        self._listsquare = [x**2 for x in self._list ]

    @property
    def listsquare(self):
        return self._listsquare
    @listsquare.setter
    def listsquare(self, val):
        self.list = [int(pow(x, 0.5)) for x in val]

>>> c = SomeClass(5)
>>> c.listsquare
[0, 1, 4, 9, 16]
>>> c.list
[0, 1, 2, 3, 4]
>>> c.list = range(0,6)
>>> c.list
[0, 1, 2, 3, 4, 5]
>>> c.listsquare
[0, 1, 4, 9, 16, 25]
>>> c.listsquare = [x**2 for x in range(0,10)]
>>> c.list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Đọc 2 phút • Python 3.7. Python 3.7—3.10

Hướng dẫn python update attribute - thuộc tính cập nhật python

Xem dưới dạng video

02:28

Đăng nhập vào tài khoản Python Barsels của bạn để lưu cài đặt screencast của bạn.

Vẫn chưa có tài khoản? Đăng ký tại đây.

Hãy nói về cách tạo thuộc tính tự động cập nhật trong Python.

Các thuộc tính không tự động thay đổi

Chúng tôi có một lớp

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
0:

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height

Khi chúng tôi gọi lớp này, chúng tôi sẽ nhận được một đối tượng

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
0:

>>> rect = Rectangle(3, 4)

Đối tượng

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
0 này có
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
3,
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
4 và
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5:

>>> rect.width
3
>>> rect.height
4
>>> rect.area
12

Nếu chúng tôi đang xem xét chấp nhận được để thay đổi

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
3 hoặc
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
4 hoặc đối tượng ____10 hiện tại, chúng tôi có thể gặp vấn đề. Nếu chúng ta thay đổi
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
3 của
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
0,
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5 sẽ không tự động thay đổi:

>>> rect.width = 10
>>> rect.area
12

Điều này xảy ra bởi vì chúng tôi chỉ gán

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5 một lần: trong phương thức khởi tạo của chúng tôi. Khi chúng tôi lần đầu tiên tạo một đối tượng
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
0, chúng tôi đặt
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5 theo phương thức
>>> rect = Rectangle(3, 4)
5 của chúng tôi và đó là nó.we only assigned the
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5 one time
: in our initializer method. When we first make a
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
0 object, we set the
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5 in our
>>> rect = Rectangle(3, 4)
5 method, and that's it.

Chúng tôi không sử dụng các phương thức getter

Trong rất nhiều ngôn ngữ lập trình, thông thường để khắc phục vấn đề này bằng cách tạo phương thức getter. Thay vì thuộc tính

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5, chúng tôi sẽ tạo một phương thức (có thể được gọi là
>>> rect = Rectangle(3, 4)
7 hoặc chỉ
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5):

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

Khi chúng tôi truy cập phương thức

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5 ngay bây giờ, chúng tôi sẽ thấy rằng đó là một phương thức ràng buộc:

>>> rect = Rectangle(3, 4)
>>> rect.area
<bound method Rectangle.area of <__main__.Rectangle object at 0x7f028eb3caf0>>

Chúng tôi không thể truy cập

>>> rect.width
3
>>> rect.height
4
>>> rect.area
12
0 để có được khu vực của chúng tôi nữa. Chúng tôi phải gọi phương thức
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5 để có được kết quả:

Bây giờ nếu chúng tôi cập nhật

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
3 của đối tượng
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
0 của chúng tôi:

Và gọi phương thức

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5:

Python sẽ tái hiện khu vực và cho chúng tôi trở lại kết quả chính xác.

Trong Python, phương pháp viết getter không phổ biến. Thay vì sử dụng phương thức Getter, chúng ta có xu hướng tạo một tài sản.writing getter methods is not common. Instead of using getter method, we tend to make a property.

Chúng tôi thích các thuộc tính hơn các phương thức getter

Chúng tôi đang sử dụng trình trang trí

>>> rect.width
3
>>> rect.height
4
>>> rect.area
12
5 ở đây:

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height

    @property
    def area(self):
        return self.width * self.height

Đó được gọi là một người trang trí vì cú pháp

>>> rect.width
3
>>> rect.height
4
>>> rect.area
12
6 đó.

Khi chúng tôi sử dụng bộ trang trí tài sản, nó sẽ biến những gì trước đây là một phương thức thành một tài sản.the property decorator, it will make what was previously a method into a property.

Bây giờ nếu chúng ta tạo một đối tượng

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
0, giống như trước đây và chúng ta truy cập thuộc tính
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5:

>>> rect = Rectangle(3, 4)
>>> rect.area
12

Không giống như trước đây, chúng ta không cần đặt dấu ngoặc đơn sau

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5! Trên thực tế, chúng ta không thể đặt dấu ngoặc đơn. Đơn giản chỉ bằng cách truy cập thuộc tính
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5, chúng tôi nhận được câu trả lời của chúng tôi.we don't need to put parentheses after
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5! In fact, we can't put parentheses. Simply by accessing the
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5 attribute we get our answer.

Vì vậy, nếu chúng tôi thay đổi

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
3 hoặc
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
4 và chúng tôi sẽ truy cập lại
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5, nó sẽ tự động kết hợp lại khu vực:

>>> rect.width = 10
>>> rect.area
40

Vì vậy, Python không thực sự lưu trữ thuộc tính

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5 ở bất cứ đâu. Thay vào đó, mỗi khi thuộc tính
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5 được truy cập, nó gọi phương thức
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5 và cung cấp cho chúng tôi bất cứ giá trị trả về nào. Và nó thực hiện điều này một cách tự động, đơn giản vì chúng tôi đã truy cập thuộc tính
>>> rect.width
3
>>> rect.height
4
>>> rect.area
12
0.isn't actually storing the
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5 attribute anywhere. Instead, every time the
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5 attribute is accessed, it calls
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = self.width * self.height
5 method and gives us back whatever the return value is. And it does this automatically, simply because we accessed the
>>> rect.width
3
>>> rect.height
4
>>> rect.area
12
0 attribute.

Bản tóm tắt

Nếu bạn muốn tạo một thuộc tính trên một đối tượng trong Python tự động cập nhật, bạn có thể sử dụng thuộc tính.

Sê -ri: Thuộc tính

Chúng tôi không sử dụng các phương thức Getter và Setter trong Python. Thay vào đó chúng tôi làm cho các thuộc tính.

Các thuộc tính cho phép chúng tôi tùy chỉnh những gì xảy ra khi bạn truy cập một thuộc tính và những gì xảy ra khi bạn gán cho một thuộc tính.

Để theo dõi tiến trình của bạn trên con đường chủ đề Python Barsels này, đăng nhập hoặc đăng ký.

Một mẹo Python mỗi tuần

Cần lấp đầy khoảng trống trong các kỹ năng trăn của bạn? Tôi gửi email hàng tuần được thiết kế để làm điều đó.fill-in gaps in your Python skills? I send weekly emails designed to do just that.

Làm thế nào để bạn thay đổi thuộc tính lớp trong Python?

Nhưng hãy cẩn thận, nếu bạn muốn thay đổi thuộc tính lớp, bạn phải làm điều đó với ký hiệu ClassName.AttributEname. Nếu không, bạn sẽ tạo một biến thể hiện mới.ClassName. AttributeName. Otherwise, you will create a new instance variable.

Làm thế nào để bạn truy cập các thuộc tính của một lớp trong Python?

Truy cập các thuộc tính của một lớp getAttr () - phương thức Python được sử dụng để truy cập thuộc tính của một lớp. HasAttr () - Một phương thức Python được sử dụng để xác minh sự hiện diện của một thuộc tính trong một lớp. setAttr () - một phương thức python được sử dụng để đặt một thuộc tính bổ sung trong một lớp.getattr() − A python method used to access the attribute of a class. hasattr() − A python method used to verify the presence of an attribute in a class. setattr() − A python method used to set an additional attribute in a class.

Thuộc tính trong lớp Python là gì?

Các thuộc tính lớp Python là các biến của một lớp được chia sẻ giữa tất cả các trường hợp của nó.Chúng khác với các thuộc tính thể hiện trong các thuộc tính đó chỉ được sở hữu bởi một trường hợp cụ thể của lớp và không được chia sẻ giữa các trường hợp.variables of a class that are shared between all of its instances. They differ from instance attributes in that instance attributes are owned by one specific instance of the class only, and are not shared between instances.

Làm thế nào để bạn khởi tạo một thuộc tính lớp trong Python?

Vì Python sẽ tự động gọi phương thức __init __ () ngay sau khi tạo một đối tượng mới, bạn có thể sử dụng phương thức __init __ () để khởi tạo các thuộc tính của đối tượng.Sau đây xác định lớp người với phương thức __init __ (): Lớp người: def __init __ (tự, tên, tuổi): self.name = tên tự.use the __init__() method to initialize the object's attributes. The following defines the Person class with the __init__() method: class Person: def __init__(self, name, age): self.name = name self.