Instances in python

前端 未结 5 790
日久生厌
日久生厌 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: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.

    0 讨论(0)
  • 2021-01-25 16:19

    The reason the value never changes is because it is a class variable meaning when the class is defined and evaluated but not when instances are made then the value is set.

    If you want the value to change make it set inside the initialiser

    0 讨论(0)
  • 2021-01-25 16:22

    let's uncomment "self.mytime = time.time()" line from your code:

    class test:
        mytime = time.time();   
        def __init__(self):
            self.newtime = time.time();
            time.sleep(1);
            #pass - no use of pass here
    
    
    test1 = test()
    test2 = test()
    
    print test1.mytime
    o/p: 1347883057.638443
    print test2.mytime
    o/p: 1347883057.638443
    

    In this case, "mytime" variable is like static class member. and all instances of test() class (i.e: test1 and test2) will share same value of class attributes.

    print test1.newtime
    o/p: 1347883063.421356
    
    print test2.newtime
    o/p: 1347883068.7103591
    

    whereas, value of instance variable. will be different for each instance of class.

    therefore, to get different timing for each instance. you need to declare instance variable using "self" in "init" method.

    • self = instance as a first parameter in class method
    • init = default constructor method in python for defined class

    Hope, it will help.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-25 16:33

    The mytime variable is first created as a class variable and not an instance variable.

    Python tries to locate a name on an instance first and if it is not found it will lookup the name on the class. You can query the namespace of any object (including) classes by looking at the object's __dict__, which is a dictionary containing the instance variables.

    As soon as you set test1.mytime = 12, you created a new name in that object's __dict__, which from then on shadowed the class variable with the same name.

    0 讨论(0)
提交回复
热议问题