简体   繁体   中英

Is there a way to set a value equal to a variable in a different class and have it keep the value from the first run and not have to run everytime?

I have two classes and one is using selenium to scrape like 25 different pages and it takes a really long time. I have another class that would take 1 second to run but its calling a variable from the other class. I want self.numbers to equal data.scores and somehow keep the value set so that testing will only take a second. The class that takes forever is AllData() and I want self.numbers to persist without having to copy and paste the printed value.

from collections import defaultdict
from data import *

class Test:
    def __init__(self,):
        data = AllData()
        self.numbers = data.scores

I'm not sure I've fully understood. Would using a class variable solve your problem? This means that you can call the current value

class a:
    var = {'init':'has', 'not':'run'}
    
    def __new__(cls):
        cls.var = {'new has':'run'}
        return super(a, cls).__new__(cls) 
    
    def __init__(self):
        self.var['so has'] = 'init'

class b:
    def __init__(self, obj):
        self.sav = obj.var

Example use:

>>> b(a).var
{'init':'has', 'not':'run'}

>>> b(a()).var
{'new has': 'run', 'so has': 'init'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM