python: What happens when class attribute, instance attribute, and method all have the same name?

前端 未结 4 1198
难免孤独
难免孤独 2020-12-02 19:02

How does python differentiate a class attribute, instance attribute, and method when the names are the same?

class Exam(object):

    test = \"class var\"

          


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-02 19:23

    You can call method as class method and pass your instance into it:

    Exam.test(test_o)
    

    Or, if you don't want use Exam:

    type(test_o).test(test_o)
    

提交回复
热议问题