简体   繁体   中英

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. 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 -

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. If I call the second function, name_insurance() , in the main or any other script, I get a different name, not the same name. The three print() statements confirm that. 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 .

Every time you NameHandle() you're creating a new instance, so, getting a new random line.

Just create the class instance once and use that instance to call the functions on.

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.

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