How to modify variables in another python file?

2024/10/10 6:20:45

windows 10 - python 3.5.2

Hi, I have the two following python files, and I want to edit the second file's variables using the code in the first python file.

firstfile.py

from X.secondfile import *def edit():#editing second file's variables by user input
if Language == 'en-US':print('language is English-us')
elif Language == 'en-UK':print('language is English-uk')

secondfile.py

Language = 'en-US'

i can add some variables to it by following code, but how can i edit one ?

with open("secondfile.py","a") as f:f.write("Language = 'en-US'")

Any ideas how to do that?

Answer

You can embed the Language in a class in the second file that has a method to change it.

Module 2

class Language:def __init__(self):self.language = 'en-US'def __str__(self):return self.languagedef change(self, lang):assert isinstance(lang, str)self.language = langlanguage = Language()

Then import the "language," and change it with the change method.

Module 1

from module2 import languageprint(language)
language.change("test")
print(language)
https://en.xdnf.cn/q/69930.html

Related Q&A

SciPy optimizer ignores one of the constraints

I am trying to solve an optimization problem where I need to create a portfolio that with a minimum tracking error from benchmark portfolio and its subject to some constraints:import scipy.optimize as …

How can one use HashiCorp Vault in Airflow?

I am starting to use Apache Airflow and I am wondering how to effectively make it use secrets and passwords stored in Vault. Unfortunately, search does not return meaningful answers beyond a yet-to-be-…

List all words in a dictionary that start with user input

How would a go about making a program where the user enters a string, and the program generates a list of words beginning with that string?Ex: User: "abd" Program:abdicate, abdomen, abduct..…

Python version of C#s conditional operator (?)

I saw this question but it uses the ?? operator as a null check, I want to use it as a bool true/false test.I have this code in Python:if self.trait == self.spouse.trait:trait = self.trait else:trait…

Python String Replace Error

I have a python script that keeps returning the following error:TypeError: replace() takes at least 2 arguments (1 given)I cannot for the life of me figure out what is causing this.Here is part of my c…

How to run two modules at the same time in IDLE

I am working on a super simple socket program and I have code for the client and code for the server. How do I run both these .py files at the same time to see if they work ?

Passing 2 dimensional C array to python numpy

I need some help regarding passing C array to python(numpy). I have 2d array of doubles NumRows x NumInputs, it seems that PyArray_SimpleNewFromData does not convert it right way - it is hard to see be…

Best way to implement numpy.sin(x) / x where x might contain 0

What I am doing now is:import numpy as npeps = np.finfo(float).epsdef sindiv(x):x = np.abs(x)return np.maximum(eps, np.sin(x)) / np.maximum(eps, x)But there is quite a lot of additional array operation…

Scrapy process.crawl() to export data to json

This might be a subquestion of Passing arguments to process.crawl in Scrapy python but the author marked the answer (that doesnt answer the subquestion im asking myself) as a satisfying one.Heres my pr…

Embedding Python in C: Error in linking - undefined reference to PyString_AsString

I am trying to embed a python program inside a C program. My OS is Ubuntu 14.04I try to embed python 2.7 and python 3.4 interpreter in the same C code base (as separate applications). The compilation a…