Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Built-in Class Attribute __name__ ใน Python ทำอะไร

Built-in Attribute นี้พิมพ์ชื่อของ Class, Type, Function, Method, Descriptor หรือ Generator Instance

ตัวอย่าง: ถ้า Python Interpreter รันโมดูลนั้นเป็น Main Program จะตั้งค่า __name__ เป็น "__main__" หากถูก Import จากโมดูลอื่น __name__ จะเป็นชื่อโมดูลนั้น

ตัวอย่าง

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

ผลลัพธ์

foo
Bar