Instances in python

前端 未结 5 795
日久生厌
日久生厌 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条回答
  •  旧时难觅i
    2021-01-25 16:19

    the mytime = time.time() is executed when the class is defined(when the interpreter runs the class-definition-code, and this will be run only once, so all instances will get the same mytime.

    if you want different mytime in different instances, you have to use the self's one, and access it with instance name rather than class name.

提交回复
热议问题