python - 如何从_init_函数python中的用户输入函数调用变量?
问题描述
我有一个包含一些启动信息的初始化函数。我想
- 让用户输入他们的电子邮件地址,
- 将其保存到名为“user_id”的变量中
- 然后在init函数中使用该变量。
它似乎正在使用全局变量,但我读过使用全局变量是一个坏主意。
如何在不使用全局变量的情况下实现这一点?
user_id ="no email yet"
Class GetDatabase():
def user_email_input():
global user_id
user_id = input("what is your email")
return
def __init__(self,uri: Text = "localhost:2555", servername: Text = "schools") -> Text:
global user_id
self.uri = uri
self.servername= servername
self.me = user_id```
解决方案
不好的方法,但如果你必须:
class GetDatabase:
def __init__(self, uri="localhost:2555", servername="schools"):
self.uri = uri
self.servername = servername
self.user_email_input()
def user_email_input(self):
self.me = input("what is your email: ")
将参数传递给类的实例化似乎正是您所需要的:
class GetDatabase:
def __init__(self, user_id, uri="localhost:2555", servername="schools"):
self.uri = uri
self.servername = servername
self.me = user_id
# then instantiated with
db = GetDatabase(my_user_id, my_uri, my_servername)
推荐阅读
- sql - 多对多关系的 SQL 连接
- javascript - 动画在预加载 CSS 和图像之前开始
- python - 在 sklearn 中使用 Leave-One-Out 交叉验证的 ROC 曲线
- docker - 如何在相同的heroku应用程序但不同的docker镜像中部署前端和后端
- c# - 我想将我的 Json 对象转换为自定义字符串以保存到数据库
- r - 将国家多边形添加到地图框
- python - 使用 kwargs(或 args)而不是简单的 dict 有什么好处?
- react-native - 如何在 redux 的动作上调用动作?
- c# - 将方法调用委托给另一个方法
- ruby - Cucumber ruby 如何使用 ParameterType 来转换给定的表达式?