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ả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 :
- Bàn luận This function is used to access the attribute of object.
- 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.
- 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.
- 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
5def x[]:
print "x"
0>>> def x[]:
... print x.__name__
...
>>> x[]
x
3def x[]:
print "x"
2 def x[]:
print "x"
3def x[]:
print "x"
4def x[]:
print "x"
5def x[]:
print "x"
6def x[]:
print "x"
7 def x[]:
print "x"
8def x[]:
print "x"
4>>> y = x
>>> y[]
x
0def x[]:
print "x"
6def x[]:
print "x"
7 def x[]:
print "x"
8def 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
8def 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
4def 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
6def 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
4Harsh True 1523
Harsh True 1524
Harsh True 1525
>>> 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 1528
>>> def x[]:
... print x.__name__
...
>>> x[]
x
34 9 90
>>> def x[]:
... print x.__name__
...
>>> x[]
x
3def x[]:
print "x"
2 4 9 93
def x[]:
print "x"
64 9 95
>>> def x[]:
... print x.__name__
...
>>> x[]
x
5 x
4 9 98
4 9 99
3 3 20
>>> def x[]:
... print x.__name__
...
>>> x[]
x
53 3 22
3 3 23
>>> def x[]:
... print x.__name__
...
>>> x[]
x
5 3 3 22
3 3 26
3 3 27
>>> 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
6def x[]:
print "x"
7 x
0x
1x
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
6def x[]:
print "x"
7 x
5def x[]:
print "x"
7 x
0Đầ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 1528
Explained with the example given below :
>>> def x[]:
... print x.__name__
...
>>> x[]
x
3def x[]:
print "x"
2 4 9 93
def x[]:
print "x"
64 9 95
>>> def x[]:
... print x.__name__
...
>>> x[]
x
5 x
4 9 98
4 9 99
3 3 23
>>> def x[]:
... print x.__name__
...
>>> x[]
x
5 3 3 22
def x[]:
print "x"
7 x
0def x[]:
print "x"
7 x
5Truy 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 x
9>>> def x[]:
... print x.__name__
...
>>> x[]
x
3def x[]:
print "x"
2 >>> def x[]:
... print x.__name__
...
>>> x[]
x
0224____24def x[]:
print "x"
5def x[]:
print "x"
6def 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
09def x[]:
print "x"
6def x[]:
print "x"
4>>> def x[]:
... print x.__name__
...
>>> x[]
x
12215 3 3 27
>>> def x[]:
... print x.__name__
...
>>> x[]
x
3def x[]:
print "x"
2 >>> def x[]:
... print x.__name__
...
>>> x[]
x
17def x[]:
print "x"
4def x[]:
print "x"
5Cá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
34def x[]:
print "x"
7>>> def x[]:
... print x.__name__
...
>>> x[]
x
62>>> def x[]:
... print x.__name__
...
>>> x[]
x
3def 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