栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从另一个函数调用在一个函数内部定义的变量

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从另一个函数调用在一个函数内部定义的变量

是的,您应该考虑在一个类中定义您的函数,并使word成为成员。这比较干净

class Spam:    def oneFunction(self,lists):        category=random.choice(list(lists.keys()))        self.word=random.choice(lists[category])    def anotherFunction(self):        for letter in self.word:print("_",end=" ")

创建类后,您必须将其实例化为对象并访问成员函数。

s = Spam()s.oneFunction(lists)s.anotherFunction()

另一种方法是使

oneFunction
单词返回,以便您可以使用
oneFunction
而不是单词in
anotherFunction

>>> def oneFunction(lists):        category=random.choice(list(lists.keys()))        return random.choice(lists[category])>>> def anotherFunction():        for letter in oneFunction(lists):print("_",end=" ")

最后,您还可以使

anotherFunction
,接受word作为参数,您可以从调用结果中传递该参数
oneFunction

>>> def anotherFunction(words):        for letter in words:print("_",end=" ")>>> anotherFunction(oneFunction(lists))


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/402175.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号