python在def()内部调用def() [英] python calling a def() inside of def()

查看:98
本文介绍了python在def()内部调用def()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的python助手打包,发现一个问题.

I'm making a package for my python assistant and have found a problem.

我将以下程序导入到主脚本中.

Im importing the following program into the main script.

import os

def load() :
    def tts(name) :
        os.system("""PowerShell -Command "Add-Type –AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak(' """ + name + " ');"

如何在我的程序中调用该函数

how do i call the function into my program

我尝试过:

import loadfile
loadfile.load().tts("petar")

它不起作用

推荐答案

您绝不应该将子功能暴露在其作用域之外,在这种情况下,您不能将 tts 方法暴露在外加载.实际上,访问 tts 而不将其引用暴露在您的 load()方法之外是不可能的.我建议您宁愿使用这样的类:

You are never supposed to expose a sub-function outside of its scope, in this case, the tts method outside load. It's actually imposible to access tts without exposing its reference outside of your load() method. I suggest you to rather use a class like this:

loadfile.py 中:

import os

class LoadFile(object):
    def tts(self, name):
        os.system("""PowerShell -Command "Add-Type –AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak(' """ + name + " ');")

def load():
    return LoadFile()

在主要代码上:导入加载文件loadfile.load().tts("petar")

这篇关于python在def()内部调用def()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆