Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Thuộc tính class built-in __name__ trong Python

__name__ in tên của class, type, function, method, descriptor, hoặc generator instance.

Nếu Python interpreter chạy module đó làm main program, set __name__ = "__main__"`. Nếu import từ module khác, __name__ là tên module.

Ví dụ

class Bar:
    def foo():
        \"\"\" This is an example of how a doc_string looks like. \"\"\"
        pass
    
    print(foo.__name__)  # foo
    print(Bar.__name__)  # Bar

Kết quả

foo
Bar