python 中的函数声明是否具有可读且干净的代码?

发布于 2024-09-27 20:43:36 字数 408 浏览 8 评论 0原文

是否可以在 python 中声明函数并稍后定义它们或在单独的文件中定义它们?

我有一些代码,例如:

class tata:
   def method1(self):
      def func1():
         #  This local function will be only used in method1, so there is no use to
         # define it outside.
         #  Some code for func1.
      # Some code for method1.

问题是代码变得混乱且难以阅读。所以我想知道是否可以在 method1 内声明 func1 并稍后定义它?

Is it possible to declare functions in python and define them later or in a separate file?

I have some code like:

class tata:
   def method1(self):
      def func1():
         #  This local function will be only used in method1, so there is no use to
         # define it outside.
         #  Some code for func1.
      # Some code for method1.

The problem is that the code becomes messy and difficult to read. So I wonder if it's possible for instance to declare func1 inside method1 and define it later?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

能怎样 2024-10-04 20:43:36

当然,没问题:

foo.py:

def func1():
    pass

script.py:

import foo
class tata:
   def method1(self):
      func1=foo.func1

Sure, no problem:

foo.py:

def func1():
    pass

script.py:

import foo
class tata:
   def method1(self):
      func1=foo.func1
红颜悴 2024-10-04 20:43:36

我认为你想要的是在method1中导入函数,例如

def method1(...):
    from module_where_func1_is_defined import func1
    # do stuff
    something = func1(stuff, more_stuff)
    # do more stuff

Python模块基本上只是文件;只要文件 module_where_func1_is_define.py 与脚本位于同一目录中,method1 就能够导入它。

如果您希望能够自定义 method1 的工作方式,并且更清楚地表明它使用 func1,您可以传递 func1method1 作为默认参数:

import other_module

# various codes

def method1(other_args, func=other_module.func1)
    # do stuff
    something = func(stuff, more_stuff)
    # do more stuff

I think what you want is to import the function within method1, e.g.

def method1(...):
    from module_where_func1_is_defined import func1
    # do stuff
    something = func1(stuff, more_stuff)
    # do more stuff

Python modules are basically just files; as long as the file module_where_func1_is_defined.py is in the same directory as your script, method1 will be able to import it.

If you want to be able to customize the way method1 works, and also make it even more clear that it uses func1, you can pass func1 to method1 as a default parameter:

import other_module

# various codes

def method1(other_args, func=other_module.func1)
    # do stuff
    something = func(stuff, more_stuff)
    # do more stuff
花期渐远 2024-10-04 20:43:36

如果 func1() 需要处理 method1() 范围内包含的任何内容,那么最好将 func1() 的定义保留在那里。如果 func1() 未在 method1() 范围内定义,则必须让 func1() 接收任何相关数据作为参数

If func1() needs to handle anything contained in the scope of method1() you're best leaving func1()'s definition there. You'll have to have func1() receive any pertinent data as parameters if it's not defined within the scope of method1()

游魂 2024-10-04 20:43:36

内部定义在内部作用域中创建一个单独的名称。它将隐藏您稍后定义的同名任何内容。如果您想稍后定义该函数,那就这样做吧。仅在实际使用时才会检查该名称。

def foo():
  print 'foo'
  bar()

def bar():
  print 'bar'

foo()

The inner definition creates a separate name in the inner scope. It will shadow anything you define later on with the same name. If you want to define the function later on, then just do so. The name will only be checked for when it is actually used.

def foo():
  print 'foo'
  bar()

def bar():
  print 'bar'

foo()
喜你已久 2024-10-04 20:43:36

您可以随后创建方法

class Something(object):
   def test1(self):
     pass

def dummy(self):
   print "ok", self

Something.test1 = dummy

但是不可能有匿名函数(嗯,有 lambda 表达式,但您不能在那里有语句),因此您必须提供一个临时名称

您可能需要使用装饰器以使其更具可读性:

def define(cls, name):
  def decor(f):
     setattr(cls, name, f)
  return decor


class Something(object):
   def test1(self):
     pass

@define(Something, "test1")
def dummy(self):
   print "ok", self

这段代码应该更具可读性。它仍然会污染 dummy,但将其初始化为 null。

You can create methods afterwards

class Something(object):
   def test1(self):
     pass

def dummy(self):
   print "ok", self

Something.test1 = dummy

However it's not possible to have an anonymous function (well, there are lambda expressions but you cannot have statements there), so you have to provide a temporary name

You might want to use decorators in order to make it more readable:

def define(cls, name):
  def decor(f):
     setattr(cls, name, f)
  return decor


class Something(object):
   def test1(self):
     pass

@define(Something, "test1")
def dummy(self):
   print "ok", self

This code should be more readable. It will still pollute dummy but initialize it with null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文