Instances in python

2019-03-01 17:00发布

问题:

I created the following example to understand the instances in python

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


from test import test

test1 = test()
test2 = test()

print test1.mytime
print test2.mytime

test1.mytime = 12

print test1.mytime
print test2.mytime

in this case the output id the following:

1347876794.72
1347876794.72
12
1347876794.72

I expected that the test2.mytime is bigger with 1 second than the test1.mytime. Why doesn't created a copy about the mytime in each instance?

回答1:

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.



回答2:

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.



回答3:

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



回答4:

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.



回答5:

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.



标签: python class