2014-10-27 20 views
1

的名稱中刪除下劃線我有兩個功能,用長名稱:我可以從一個Python測試功能

# ruby 
def test_write_method_raises_value_error 

# python 
def test_write_method_raises_value_error(self): 

在紅寶石我可以rename的功能如下:

test 'write method should raise value error' do 
    # rest of function 

有在Python中可比較的構造?

+2

不,那會不會是在Python函數名的合法性。 – davidism 2014-10-27 13:03:25

+1

你可以做的是將方法保存在一個變量中。 – 2014-10-27 13:04:40

+0

你可以刪除它,但pep8說它不是Python風格。請檢查這個http://legacy.python.org/dev/peps/pep-0008/#function-names。 Python不會給出錯誤,但不是它的味道。 – Nilesh 2014-10-27 13:06:58

回答

1

如果您只是爲了測試目的而縮短功能名稱,可以輕鬆完成。

一切都是python中的對象,甚至函數。你可以將它分配給變量,這實際上只是一個標籤。

例如:

def very_very_long_function_name_that_we_want_to_test(argument): 
    pass 


# test: 
testable = very_very_long_function_name_that_we_want_to_test 

# This will call our "long function" 
testable('test') 

但是,新的這個函數的名稱應該按照Python的命名約束是正確的,不能有空格例如