Hướng dẫn is self in python the same as this? - Bản thân trong python có giống như thế này không?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận

    Tự đại diện cho trường hợp của lớp. Bằng cách sử dụng bản thân của người Viking & NBSP;, chúng ta có thể truy cập các thuộc tính và phương thức của lớp trong Python. Nó liên kết các thuộc tính với các đối số đã cho.

    Lý do bạn cần sử dụng bản thân. là bởi vì Python không sử dụng @ Cú pháp để chỉ các thuộc tính thể hiện. Python quyết định thực hiện các phương thức theo cách làm cho thể hiện phương thức được truyền tự động, nhưng không nhận được tự động: tham số đầu tiên của các phương thức là phiên bản phương thức được gọi.

    Python3

    Theo cách rõ ràng hơn, bạn có thể nói rằng bản thân có đặc điểm sau-

    Bản thân luôn chỉ vào đối tượng hiện tại.

    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    0
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    1
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    2
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    3
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    4
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    5
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    2self
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    8

    class check:

    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    1
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    2
    we have used another parameter name in place of self
    4
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    4
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    5
    we have used another parameter name in place of self
    7

        def __init__(self

    Address of self =  140124194801032
    Address of class object =  140124194801032

    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    9
    we have used another parameter name in place of self
    0
    we have used another parameter name in place of self
    1

    Python3

    Đầu ra

    Một ví dụ khác về việc sử dụng bản thân:

    class

    we have used another parameter name in place of self
    9

        def __init__(selfclass4

    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    0selfclass7
    we have used another parameter name in place of self
    0 class9

    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    0
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    1
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    2    3    4self    6

    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    0
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    1
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    2def0    4selfdef3

    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    0selfcheck:2
    we have used another parameter name in place of self
    0 check:4

        def check:7self__

    __init__(8

    __init__(9

    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    1
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    2self2self3

    def4

    we have used another parameter name in place of self
    0 def6def7    4__6

        def __init__(self

    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green

    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    9
    we have used another parameter name in place of self
    0
    we have used another parameter name in place of self
    1

    Đầu ra

    Python3

    Theo cách rõ ràng hơn, bạn có thể nói rằng bản thân có đặc điểm sau-

    Bản thân luôn chỉ vào đối tượng hiện tại.

    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    0
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    1
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    2):6__init__(0

    class check:

        def __init__(self

    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    05

    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    9
    we have used another parameter name in place of self
    0
    we have used another parameter name in place of self
    1

    Đầu ra

    Một ví dụ khác về việc sử dụng bản thân:

    class

    we have used another parameter name in place of self
    9

        def __init__(selfclass4

    Python3

    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    0selfclass7
    we have used another parameter name in place of self
    0 class9

    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    0selfcheck:2
    we have used another parameter name in place of self
    0 check:4

    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    0
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    1
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    2
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    31

    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    0
    Model is audi a4
    color is blue
    Model is ferrari 488
    color is green
    33__init__(0

        def check:7self__

        def __init__(self

    we have used another parameter name in place of self


    Có phải là bản thân Python giống như C ++ này không?

    Bản thân trong Python tương đương với con trỏ tự trong C ++ và tham chiếu này trong Java và C#.Bạn phải tự hỏi làm thế nào Python mang lại giá trị cho bản thân và tại sao bạn không cần phải đưa ra một giá trị cho nó.Một ví dụ sẽ làm cho điều này rõ ràng.Giả sử bạn có một lớp gọi là MyClass và một thể hiện của lớp này được gọi là myObject. and the this reference in Java and C#. You must be wondering how Python gives the value for self and why you don't need to give a value for it. An example will make this clear. Say you have a class called MyClass and an instance of this class called MyObject .

    Bản thân trong Python có nghĩa là gì?

    Bản thân được sử dụng để đại diện cho thể hiện của lớp.Với từ khóa này, bạn có thể truy cập các thuộc tính và phương thức của lớp trong Python.Nó liên kết các thuộc tính với các đối số đã cho.Lý do tại sao chúng ta sử dụng bản thân là Python không sử dụng cú pháp '@' để chỉ các thuộc tính thể hiện.