[英]Keep one variable the same within the class (with multiple functions) for reuse elsewhere
I have a class script ( secondary.py
), that when called in my main.py
file, works.我有一个 class 脚本( secondary.py
),当在我的main.py
文件中调用它时,它可以工作。 My issue is that I want to call the second function below in another script, tertiary.py
, and that the output variable reg_name
be the same for both of the functions below -我的问题是我想在另一个脚本tertiary.py
中调用下面的第二个 function ,并且 output 变量reg_name
对于下面的两个函数都相同 -
import pandas as pd
import re
# Class to keep randomly pulled name the same throughout ----
class NameHandle():
# init
def __init__(self):
# reads in csv with a celeb's name in one col & their twitter handle in the second col - e.g. name: Oprah & handle: @oprah
# kicker: since the row is randomly chosen, it *needs* to stay the same throughout
handle_csv = pd.read_csv('handle_list.csv')
ran_line = handle_csv.sample(n = 1, replace = False)
self.reg_name = ran_line['name'].to_string()
print("test one" + self.reg_name) ### test print
self.handle_tag = ran_line['handle'].to_string()
# format randomly pulled celeb name and twitter handle
def name_and_handle(self):
# create & format sentence, to send back to main.py
print("test two" + self.reg_name) ### test print
sentence = self.reg_name + "'s Twitter handle is" + ' ' + self.handle_tag
sentence = re.sub(r'\d+', '', sentence)
sentence = " ".join(re.split("\s+", sentence, flags = re.UNICODE))
return sentence
# function to keep celeb name the same through the entire process
def name_insurance(self):
same_name = self.reg_name
print("test three" + same_name) ### test print
return same_name
# Main function ----
if __name__ == '__main__':
NameHandle().name_and_handle()
NameHandle().name_insurance()
If I call the function name_and_handle()
in the main.py
script - it works.如果我在main.py
脚本中调用 function name_and_handle()
- 它可以工作。 If I call the second function, name_insurance()
, in the main or any other script, I get a different name, not the same name.如果我在主脚本或任何其他脚本中调用第二个 function, name_insurance()
,我会得到一个不同的名称,而不是相同的名称。 The three print()
statements confirm that.三个print()
语句证实了这一点。 I'm missing something easy - very new to classes - if this is a stupid question, crucify me below, and I'll delete.我错过了一些简单的东西 - 对课程来说非常新 - 如果这是一个愚蠢的问题,请在下面将我钉在十字架上,我将删除。
# Calling `secondary.py` in `main.py`
import secondary as sd
# outputs different names
sd.NameHandle().name_and_handle()
sd.NameHandle().name_insurance()
So, you've got ran_line = handle_csv.sample(n = 1, replace = False)
meaning you'll get a random line every time you create a new instance of your class NameHandler
.因此,您得到了ran_line = handle_csv.sample(n = 1, replace = False)
这意味着每次创建 class NameHandler
的新实例时都会得到一条随机线。
Every time you NameHandle()
you're creating a new instance, so, getting a new random line.每次您NameHandle()
时,您都在创建一个新实例,因此,得到一个新的随机行。
Just create the class instance once and use that instance to call the functions on.只需创建一次 class 实例并使用该实例调用函数。
name_handle_instance = sd.NameHandle()
name_handle_instance.name_and_handle()
name_handle_instance.name_insurance()
I don't know why it would work under __main__
but not when imported.我不知道为什么它会在__main__
下工作,但在导入时却不行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.