简体   繁体   English

在 class(具有多种功能)中保持一个变量相同,以便在其他地方重复使用

[英]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.

相关问题 在 for 循环中重复使用相同的变量名两次 - Reuse same variable name twice within a for loop 如何在测试中为多个函数重用相同的模拟 - How do I reuse same mocks for multiple functions in testing 如果它们在列中具有相同的值并且其中至少一个包含另一列中的字符串,如何保留多行 - How to keep multiple rows if they have the same value within a column AND at least one of them contains a string in another column 在类中的函数之间传递变量 - Pass a variable between functions within a class 如何在基于django类的视图中从同一个类中的另一个方法访问一个方法的变量 - how to access variable of one method from another method within the same class in django class based views 如何在别处使用类方法中的变量 - How to use a variable from a class method elsewhere 如何在python的同一类中的另一个函数中调用一个函数中定义的变量 - How to call a variable defined in one function in another function within same class in python 如何在Python中的同一类中的另一个方法中传递一个方法的变量 - How to pass variable of one method in other method within the same class in Python 在同一类中的方法中调用变量 - call variable in method within the same class 访问同一 class 的嵌套 function 中的变量 - Accessing a variable within a nested function of the same class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM