简体   繁体   English

python 在 def() 内部调用 def()

[英]python calling a def() inside of def()

I'm making a package for my python assistant and have found a problem.我正在为我的 python 助手制作 package 并发现问题。

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我如何将 function 调用到我的程序中

ive tried:我试过了:

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

and it didn't work它没有用

You are never supposed to expose a sub-function outside of its scope, in this case, the tts method outside load .您永远不应该在其 scope 之外公开子功能,在这种情况下,在load之外的tts方法。 It's actually imposible to access tts without exposing its reference outside of your load() method.如果不将其引用暴露在load()方法之外,实际上是不可能访问tts的。 I suggest you to rather use a class like this:我建议你宁愿使用这样的 class :

In loadfile.py :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()

On main code: import loadfile loadfile.load().tts("petar")在主代码上: import loadfile loadfile.load().tts("petar")

When you run loadfile.load().tts("petar") , it is equivalent to:当您运行loadfile.load().tts("petar")时,它相当于:

v = loadfile.load()
v.tts("petar")

Your method loadfile.load() does not return any value, so v is assigned None .您的方法loadfile.load()不返回任何值,因此v被分配None Then you try to call tts() against None , which is an error.然后您尝试针对None调用tts() ,这是一个错误。

Why are you trying to do this?你为什么要这样做? Maybe you want to create a class?也许您想创建一个 class?

you can follow this code to call def within def您可以按照此代码在 def 中调用 def

def num1(x):
   def num2(y):
      return x * y
   return num2
res = num1(10)

print(res(5))

Reference URL参考URL

You also can use the decorator "@classmethod" to create some funcion inside your class that doesn't need create an instance of your class.您还可以使用装饰器“@classmethod”在 class 中创建一些不需要创建 class 实例的函数。

In your "my_package.py" file you can do some like this:在您的“my_package.py”文件中,您可以执行以下操作:

class Loaders():

   @classmethod
   def load(self, parameter):
      return self.tts(parameter)

   def tts(self):
      os.system("""PowerShell...""")
      ...
      return self

In python files that will call this "my_package.py" you can code:在将调用此“my_package.py”的 python 文件中,您可以编码:

       filename        classname
          |                |
          V                V
from my_package import Loaders

# to use your method tts you can Instantiate your class and after call the method:
x = Loaders()
x.tts('petar')

# or you can call directly some classmethod
x = Loaders.load('petar')

If you need change the steps when initializing your class, ... you can edit the instantiation method init如果您在初始化 class 时需要更改步骤,...您可以编辑实例化方法init


class Loaders():

   def __init__(self, some_param):
      ...do something...

      return self

...
# so when you when you call 
x = Loaders()

# " ... do something ..." will do and the class object (self) will return.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM