【问题标题】:Passing objects as parameters into functions in Python将对象作为参数传递给 Python 中的函数
【发布时间】:2017-06-04 18:16:04
【问题描述】:

我是 Python 的新手,到目前为止我只使用过 VBA。我想知道是否有办法将类对象的实例作为参数/参数传递给函数。我想要一些类似我在下面复制的东西

#ClassOne.py 
class Spnt(object):
    def __init__ (self, wl, val):
    self.wl = wl
    self.val = val


#AnotherFile.py 
    import ClassOne
    def func(obj1, obj2) 
#where obj1 and obj2 are instances of the object     Spnt()
# .....code which does some math
#    returns obj3 which is another instance of the Class object Spnt()

感谢您的阅读

【问题讨论】:

标签: python class object parameters arguments


【解决方案1】:

你要找的是闭包和装饰器,我在这里举一个简单的例子:

#Decorators are an example of closures

def decorator(function_as_parameter):
    def wrapped_function():
        print "function is being called"
        function_as_parameter()
        print "function call is finished"
    return wrapped_function

@decorator
def my_function():
    print "Hello Wolrd"

my_function()

您可以自定义您想要的重要程度和理解概念 python 中的闭包是另一个函数中的包装函数。

请给我一票我需要它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    相关资源
    最近更新 更多