Instances in python

前端 未结 5 791
日久生厌
日久生厌 2021-01-25 15:47

I created the following example to understand the instances in python

import time;
class test:
    mytime = time.time();   
    def __init__(self):
        #self         


        
5条回答
  •  执笔经年
    2021-01-25 16:31

    Let's look at those lines:

    class test:
       mytime = time.time();   
    

    Here you set class member value, which is calculated once when class definition is executed, so time.time() is calculated once when module that contains class test is loaded.

    Every instance of the class test will receive that precalculated value and you must override that value (e.g. in __init__ method), accessing it via self (which is a special argument that stores reference to an instance), thus setting instance member value.

提交回复
热议问题