2017-01-19 159 views
-1

我是Python的新手,迄今爲止我只使用過VBA。我想知道是否有辦法將類對象的實例作爲參數/參數傳遞給函數。我想有像我所抄錄如下在Python中將對象作爲參數傳遞給函數

#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() 

感謝您閱讀

+2

你*自己試試這個嗎? –

+0

歡迎來到Stack Overflow!您可以先[參觀](http://stackoverflow.com/tour)並學習[如何提出一個好問題](http://stackoverflow.com/help/how-to-ask)並創建一個[最小,完整和可驗證](http://stackoverflow.com/help/mcve)示例。這使我們更容易幫助你。 –

回答

0

你所尋找的是閉包和裝飾,我將在這裏給出一個簡單的例子:

#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中的封閉是另一個封裝的功能。

請給我的投票我需要它

相關問題