Hướng dẫn how do you access a function object in python? - làm thế nào để bạn truy cập một đối tượng chức năng trong python?

Các chức năng trong Python là các đối tượng và vì nó xảy ra, những đối tượng đó có một thuộc tính chứa tên mà chúng được xác định với:

>>> def x[]:
...     pass
...
>>> print x.__name__
x

Vì vậy, một cách tiếp cận ngây thơ có thể là thế này:

>>> def x[]:
...     print x.__name__
... 
>>> x[]
x

Điều đó dường như hoạt động. Tuy nhiên, vì bạn phải biết tên của x bên trong chức năng để làm điều đó, bạn đã không thực sự đạt được bất cứ điều gì; Bạn có thể đã làm điều này:

def x[]:
    print "x"

Tuy nhiên, trên thực tế, nó còn tệ hơn thế, bởi vì thuộc tính ____1010 chỉ đề cập đến tên mà hàm được xác định. Nếu nó bị ràng buộc với một tên khác, nó sẽ không hoạt động như bạn mong đợi:

>>> y = x
>>> y[]
x

Thậm chí tệ hơn, nếu tên ban đầu không còn xung quanh, nó sẽ không hoạt động cả:

>>> del x
>>> y[]
Traceback [most recent call last]:
  File "", line 1, in 
  File "", line 2, in x
NameError: global name 'x' is not defined

Vấn đề thứ hai này là một vấn đề bạn thực sự có thể có được xung quanh, mặc dù nó không đẹp. Bí quyết là viết một người trang trí có thể chuyển tên của chức năng vào đó như một đối số:

>>> from functools import wraps
>>> def introspective[func]:
...     __name__ = func.__name__
...     @wraps[func]
...     def wrapper[*args, **kwargs]:
...         return func[__name__=__name__, *args, **kwargs]
...     return wrapper
... 
>>> @introspective
... def x[__name__]:
...     print __name__
... 
>>> x[]
x
>>> y = x
>>> y[]
x
>>> del x
>>> y[]
x

... Mặc dù như bạn có thể thấy, bạn vẫn chỉ lấy lại tên mà hàm được xác định, chứ không phải là cái nó bị ràng buộc ngay bây giờ.

Trong thực tế, câu trả lời ngắn [và chính xác] là "đừng làm vậy". Đó là một thực tế cơ bản của Python mà các đối tượng không biết tên [hoặc tên] nào họ bị ràng buộc - nếu bạn nghĩ rằng chức năng của bạn cần thông tin đó, bạn đang làm gì đó sai. It's a fundamental fact of Python that objects don't know what name [or names] they're bound to - if you think your function needs that information, you're doing something wrong.

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

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc
    Attributes of a class can also be accessed using the following built-in methods and functions :

    1. Bàn luận This function is used to access the attribute of object.
    2. Các thuộc tính của một lớp là các đối tượng hàm xác định các phương thức tương ứng của các trường hợp của nó. Chúng được sử dụng để thực hiện các điều khiển truy cập của các lớp.Attribution của một lớp cũng có thể được truy cập bằng các phương thức và chức năng tích hợp sau: This function is used to check if an attribute exist or not.
    3. setAttr [] - Hàm này được sử dụng để đặt thuộc tính. Nếu thuộc tính không tồn tại, thì nó sẽ được tạo ra. This function is used to set an attribute. If the attribute does not exist, then it would be created.
    4. delattr [] - Hàm này được sử dụng để xóa một thuộc tính. Nếu bạn đang truy cập thuộc tính sau khi xóa, nó sẽ tăng lỗi, lớp không có thuộc tính nào. This function is used to delete an attribute. If you are accessing the attribute after deleting it raises error “class has no attribute”.

    Các phương pháp sau được giải thích với ví dụ được đưa ra dưới đây:

    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    1
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    2

    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    3
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    4
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    5
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    6

    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    3
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    8
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    5
    def x[]:
        print "x"
    
    0

    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    3
    def x[]:
        print "x"
    
    2
    def x[]:
        print "x"
    
    3
    def x[]:
        print "x"
    
    4
    def x[]:
        print "x"
    
    5

    def x[]:
        print "x"
    
    6
    def x[]:
        print "x"
    
    7
    def x[]:
        print "x"
    
    8
    def x[]:
        print "x"
    
    4
    >>> y = x
    >>> y[]
    x
    
    0

    def x[]:
        print "x"
    
    6
    def x[]:
        print "x"
    
    7
    def x[]:
        print "x"
    
    8
    def x[]:
        print "x"
    
    4
    >>> y = x
    >>> y[]
    x
    
    5

    >>> y = x
    >>> y[]
    x
    
    6
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    5
    >>> y = x
    >>> y[]
    x
    
    8

    def x[]:
        print "x"
    
    7
    def x[]:
        print "x"
    
    8
    >>> del x
    >>> y[]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 2, in x
    NameError: global name 'x' is not defined
    
    1
    >>> del x
    >>> y[]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 2, in x
    NameError: global name 'x' is not defined
    
    2
    >>> del x
    >>> y[]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 2, in x
    NameError: global name 'x' is not defined
    
    3
    >>> del x
    >>> y[]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 2, in x
    NameError: global name 'x' is not defined
    
    4

    def x[]:
        print "x"
    
    7
    def x[]:
        print "x"
    
    8
    >>> del x
    >>> y[]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 2, in x
    NameError: global name 'x' is not defined
    
    7
    >>> del x
    >>> y[]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 2, in x
    NameError: global name 'x' is not defined
    
    2
    >>> del x
    >>> y[]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 2, in x
    NameError: global name 'x' is not defined
    
    3
    >>> del x
    >>> y[]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 2, in x
    NameError: global name 'x' is not defined
    
    4

    >>> from functools import wraps
    >>> def introspective[func]:
    ...     __name__ = func.__name__
    ...     @wraps[func]
    ...     def wrapper[*args, **kwargs]:
    ...         return func[__name__=__name__, *args, **kwargs]
    ...     return wrapper
    ... 
    >>> @introspective
    ... def x[__name__]:
    ...     print __name__
    ... 
    >>> x[]
    x
    >>> y = x
    >>> y[]
    x
    >>> del x
    >>> y[]
    x
    
    1
    >>> del x
    >>> y[]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 2, in x
    NameError: global name 'x' is not defined
    
    2
    >>> from functools import wraps
    >>> def introspective[func]:
    ...     __name__ = func.__name__
    ...     @wraps[func]
    ...     def wrapper[*args, **kwargs]:
    ...         return func[__name__=__name__, *args, **kwargs]
    ...     return wrapper
    ... 
    >>> @introspective
    ... def x[__name__]:
    ...     print __name__
    ... 
    >>> x[]
    x
    >>> y = x
    >>> y[]
    x
    >>> del x
    >>> y[]
    x
    
    3
    >>> from functools import wraps
    >>> def introspective[func]:
    ...     __name__ = func.__name__
    ...     @wraps[func]
    ...     def wrapper[*args, **kwargs]:
    ...         return func[__name__=__name__, *args, **kwargs]
    ...     return wrapper
    ... 
    >>> @introspective
    ... def x[__name__]:
    ...     print __name__
    ... 
    >>> x[]
    x
    >>> y = x
    >>> y[]
    x
    >>> del x
    >>> y[]
    x
    
    4
    >>> from functools import wraps
    >>> def introspective[func]:
    ...     __name__ = func.__name__
    ...     @wraps[func]
    ...     def wrapper[*args, **kwargs]:
    ...         return func[__name__=__name__, *args, **kwargs]
    ...     return wrapper
    ... 
    >>> @introspective
    ... def x[__name__]:
    ...     print __name__
    ... 
    >>> x[]
    x
    >>> y = x
    >>> y[]
    x
    >>> del x
    >>> y[]
    x
    
    5
    >>> from functools import wraps
    >>> def introspective[func]:
    ...     __name__ = func.__name__
    ...     @wraps[func]
    ...     def wrapper[*args, **kwargs]:
    ...         return func[__name__=__name__, *args, **kwargs]
    ...     return wrapper
    ... 
    >>> @introspective
    ... def x[__name__]:
    ...     print __name__
    ... 
    >>> x[]
    x
    >>> y = x
    >>> y[]
    x
    >>> del x
    >>> y[]
    x
    
    6

    def x[]:
        print "x"
    
    7
    def x[]:
        print "x"
    
    8
    >>> del x
    >>> y[]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 2, in x
    NameError: global name 'x' is not defined
    
    1
    >>> del x
    >>> y[]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 2, in x
    NameError: global name 'x' is not defined
    
    2
    >>> from functools import wraps
    >>> def introspective[func]:
    ...     __name__ = func.__name__
    ...     @wraps[func]
    ...     def wrapper[*args, **kwargs]:
    ...         return func[__name__=__name__, *args, **kwargs]
    ...     return wrapper
    ... 
    >>> @introspective
    ... def x[__name__]:
    ...     print __name__
    ... 
    >>> x[]
    x
    >>> y = x
    >>> y[]
    x
    >>> del x
    >>> y[]
    x
    
    3
    >>> del x
    >>> y[]
    Traceback [most recent call last]:
      File "", line 1, in 
      File "", line 2, in x
    NameError: global name 'x' is not defined
    
    4

    Harsh
    True
    152
    3
    Harsh
    True
    152
    4
    Harsh
    True
    152
    5
    >>> from functools import wraps
    >>> def introspective[func]:
    ...     __name__ = func.__name__
    ...     @wraps[func]
    ...     def wrapper[*args, **kwargs]:
    ...         return func[__name__=__name__, *args, **kwargs]
    ...     return wrapper
    ... 
    >>> @introspective
    ... def x[__name__]:
    ...     print __name__
    ... 
    >>> x[]
    x
    >>> y = x
    >>> y[]
    x
    >>> del x
    >>> y[]
    x
    
    6

    Đầu ra:

    Harsh
    True
    152

    Phương pháp tĩnh: Phương pháp tĩnh là một phương thức [hàm thành viên] không sử dụng đối số tự. Để khai báo một phương thức tĩnh, hãy tiến hành câu lệnh của câu lệnh @staticmethod.A static method is a method[member function] that don’t use argument self at all. To declare a static method, proceed it with the statement “@staticmethod”.

    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    1
    Harsh
    True
    152
    8

    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    3
    4
    9
    9
    0

    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    3
    def x[]:
        print "x"
    
    2
    4
    9
    9
    3

    def x[]:
        print "x"
    
    6
    4
    9
    9
    5
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    5 x
    4
    9
    9
    8
    4
    9
    9
    9

    3
    3
    2
    0
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    5
    3
    3
    2
    2

    3
    3
    2
    3
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    5
    3
    3
    2
    2

    3
    3
    2
    6
    3
    3
    2
    7
    >>> from functools import wraps
    >>> def introspective[func]:
    ...     __name__ = func.__name__
    ...     @wraps[func]
    ...     def wrapper[*args, **kwargs]:
    ...         return func[__name__=__name__, *args, **kwargs]
    ...     return wrapper
    ... 
    >>> @introspective
    ... def x[__name__]:
    ...     print __name__
    ... 
    >>> x[]
    x
    >>> y = x
    >>> y[]
    x
    >>> del x
    >>> y[]
    x
    
    6

    def x[]:
        print "x"
    
    7 x0

    x1x2

    >>> from functools import wraps
    >>> def introspective[func]:
    ...     __name__ = func.__name__
    ...     @wraps[func]
    ...     def wrapper[*args, **kwargs]:
    ...         return func[__name__=__name__, *args, **kwargs]
    ...     return wrapper
    ... 
    >>> @introspective
    ... def x[__name__]:
    ...     print __name__
    ... 
    >>> x[]
    x
    >>> y = x
    >>> y[]
    x
    >>> del x
    >>> y[]
    x
    
    6

    def x[]:
        print "x"
    
    7 x5

    def x[]:
        print "x"
    
    7 x0

    Đầu ra:

    4
    9
    9

    Phương pháp tĩnh: Phương pháp tĩnh là một phương thức [hàm thành viên] không sử dụng đối số tự. Để khai báo một phương thức tĩnh, hãy tiến hành câu lệnh của câu lệnh @staticmethod.

    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    1
    Harsh
    True
    152
    8
    Explained with the example given below :

    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    3
    def x[]:
        print "x"
    
    2
    4
    9
    9
    3

    def x[]:
        print "x"
    
    6
    4
    9
    9
    5
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    5 x
    4
    9
    9
    8
    4
    9
    9
    9

    3
    3
    2
    3
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    5
    3
    3
    2
    2

    def x[]:
        print "x"
    
    7 x0

    def x[]:
        print "x"
    
    7 x5

    Truy cập các thuộc tính và phương thức của một lớp trong một lớp khác

    Truy cập các thuộc tính và phương thức của một lớp trong một lớp khác được thực hiện bằng cách chuyển đối tượng của một lớp sang một lớp khác. Giải thích bằng ví dụ được đưa ra dưới đây:

    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    1 x9

    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    3
    def x[]:
        print "x"
    
    2
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    0224____24
    def x[]:
        print "x"
    
    5

    def x[]:
        print "x"
    
    6
    def x[]:
        print "x"
    
    4
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    07
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    5
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    09

    def x[]:
        print "x"
    
    6
    def x[]:
        print "x"
    
    4
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    12215
    3
    3
    2
    7

    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    3
    def x[]:
        print "x"
    
    2
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    17
    def x[]:
        print "x"
    
    4
    def x[]:
        print "x"
    
    5

    Các

    def x[]:
        print "x"
    
    6
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    30
    def x[]:
        print "x"
    
    4
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    32

    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    1
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    34

    def x[]:
        print "x"
    
    7
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    62

    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    3
    def x[]:
        print "x"
    
    2
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    022____24
    >>> def x[]:
    ...     print x.__name__
    ... 
    >>> x[]
    x
    
    39

    Đầu ra:

    3
    3
    2

    Làm thế nào để bạn truy cập các chức năng trong Python?

    Bốn bước để xác định một hàm trong Python là như sau:..
    Sử dụng từ khóa def để khai báo chức năng và theo dõi tên này với tên chức năng ..
    Thêm tham số vào hàm: Chúng nên nằm trong dấu ngoặc đơn của hàm. ....
    Thêm các câu lệnh mà các chức năng nên thực thi ..

    Làm thế nào để bạn gọi một đối tượng hàm trong Python?

    Để sử dụng các hàm trong Python, bạn viết tên hàm [hoặc biến trỏ đến đối tượng hàm] theo sau là dấu ngoặc đơn [để gọi hàm].Nếu hàm đó chấp nhận các đối số [như hầu hết các hàm], thì bạn sẽ chuyển các đối số bên trong dấu ngoặc đơn khi bạn gọi hàm.write the function name [or the variable that points to the function object] followed by parentheses [to call the function]. If that function accepts arguments [as most functions do], then you'll pass the arguments inside the parentheses as you call the function.

    Làm thế nào để bạn truy cập một đối tượng trong Python?

    getAttr [] - Hàm này được sử dụng để truy cập thuộc tính của đối tượng.HasAttr [] - Hàm này được sử dụng để kiểm tra xem thuộc tính có tồn tại hay không.setAttr [] - Hàm này được sử dụng để đặt thuộc tính.Nếu thuộc tính không tồn tại, thì nó sẽ được tạo ra. – This function is used to access the attribute of object. hasattr[] – This function is used to check if an attribute exist or not. setattr[] – This function is used to set an attribute. If the attribute does not exist, then it would be created.

    Làm thế nào để bạn truy cập một chức năng?

    C ++: Truy cập hàm chức năng do người dùng xác định phải được gọi rõ ràng bằng cách sử dụng tên của nó và các đối số cần thiết được truyền.Hàm do người dùng xác định nên được gọi rõ ràng bằng cách sử dụng tên của nó và các đối số cần thiết được truyền.The user-defined function should be called explicitly using its name and the required arguments to be passed. The user-defined function should be called explicitly using its name and the required arguments to be passed.

    Bài Viết Liên Quan

    Chủ Đề