讓假設我有在python此功能:函數返回一定基於規則(對象或值)
def get_thing(rule):
if rule == 0:
return 5 #<<<< int
elif rule == 1:
return 'Hello' #<<<<< string
else:
return Student() #<<<<< Object
上述功能返回基於一個不同的值(int或串或對象)規則。
我應該使用上述功能或使用此功能:
def get_int()
return 5
def get_string()
return 'Hello'
def get_student()
return Student()
我的意思是Python的一個函數返回不同的數據類型或值?
在java中,你只能返回一個類型Ex:String only。
我希望你能理解我的問題:)
謝謝。
編輯: 其他例子
def get_person(id):
if #id is belongs to student:
return Student()
elif #id belongs to teacher
return Teacher()
你需要什麼? – piokuc 2013-05-11 23:57:02
我想你將不得不爲這個問題提供更多的上下文。這就是說,第一個片段看起來不是一個好的解決方案。 – Jared 2013-05-11 23:58:08
http://stackoverflow.com/a/1839350/834998 – Greg 2013-05-12 00:01:43