[英]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.我有两个课程,一个是使用 selenium 来抓取 25 个不同的页面,这需要很长时间。 I have another class that would take 1 second to run but its calling a variable from the other class.
我有另一个 class 需要 1 秒才能运行,但它从另一个 class 调用一个变量。 I want self.numbers to equal data.scores and somehow keep the value set so that testing will only take a second.
我希望 self.numbers 等于 data.scores 并以某种方式保持值设置,以便测试只需要一秒钟。 The class that takes forever is AllData() and I want self.numbers to persist without having to copy and paste the printed value.
永远需要的 class 是 AllData() ,我希望 self.numbers 持续存在而不必复制和粘贴打印的值。
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?
使用 class 变量会解决您的问题吗? 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'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.