2005年02月04日 星期五 09:51
哈哈哈!也是个加强自学的好方法, 原先啄木鸟中的 PyGame 收集算是一个吧! 不过,这样纠缠细节,对于大型项目后期的重构优化有莫大的好处, 但是对于初学则有些舍本逐未是也乎………… On Thu, 3 Feb 2005 21:30:08 +0800, Wang Kebo <mep_ at 163.com> wrote: > > 以下的代码是从limodou的blog上截下来的。不过,总觉得这段代码不是太优雅, > 谁可以想到更加pythonic的实现方式?我觉得这样的讨论,能使我们更加熟悉 > python的特性。 > 如果谁有精力,可以建立一个Python Quiz,类似于Ruby Quiz > http://www.rubyquiz.com/ > 或Perl Quiz, > http://perl.plover.com/qotw/ > 每星期出个小题目大家来讨论,看看怎样才是最优雅的实现方式。 > > > > > def getHomeDir(): > ''' Try to find user's home directory, otherwise return current > directory.''' > try: > path1=os.path.expanduser("~") > except: > path1="" > try: > path2=os.environ["HOME"] > except: > path2="" > try: > path3=os.environ["USERPROFILE"] > except: > path3="" > > if not os.path.exists(path1): > if not os.path.exists(path2): > if not os.path.exists(path3): > return os.getcwd() > else: return path3 > else: return path2 > else: return path1 > > > > > > __ > Best Regards, > Wang Kebo > > HomePage: http://www.huihoo.org/~mep > Blog: http://sharing.blogbus.com > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > > > -- [Time is unimportant, only life important!]
2005年02月04日 星期五 10:22
我认为这个问题还是有普遍意义的,这是我的解法,抛砖引玉。
def func1():
    raise Exception
    return 'func1'
def func2():
    return ''
def func3():
    return 'func3'
def selectFirstTrue():
    for expression in ['func1()','func2()','func3()']:
        try:
            exec 'result=%s'%expression
        except:
            continue
        if result:
            return result
print selectFirstTrue()
Zoom Quiet wrote:
>哈哈哈!也是个加强自学的好方法,
>原先啄木鸟中的 PyGame 收集算是一个吧!
>
>不过,这样纠缠细节,对于大型项目后期的重构优化有莫大的好处,
>但是对于初学则有些舍本逐未是也乎…………
>
>
>On Thu, 3 Feb 2005 21:30:08 +0800, Wang Kebo <mep_ at 163.com> wrote:
>  
>
>> 
>>以下的代码是从limodou的blog上截下来的。不过,总觉得这段代码不是太优雅, 
>>谁可以想到更加pythonic的实现方式?我觉得这样的讨论,能使我们更加熟悉 
>>python的特性。 
>>如果谁有精力,可以建立一个Python Quiz,类似于Ruby Quiz 
>>http://www.rubyquiz.com/ 
>>或Perl Quiz, 
>>http://perl.plover.com/qotw/ 
>>每星期出个小题目大家来讨论,看看怎样才是最优雅的实现方式。 
>>  
>> 
>> 
>>
>>def getHomeDir():
>>    ''' Try to find user's home directory, otherwise return current
>>directory.'''
>>    try:
>>        path1=os.path.expanduser("~")
>>    except:
>>        path1=""
>>    try:
>>        path2=os.environ["HOME"]
>>    except:
>>        path2=""
>>    try:
>>        path3=os.environ["USERPROFILE"]
>>    except:
>>        path3="" 
>>
>>    if not os.path.exists(path1):
>>        if not os.path.exists(path2):
>>            if not os.path.exists(path3):
>>                return os.getcwd()
>>            else: return path3
>>        else: return path2
>>    else: return path1 
>>  
>> 
>>
>> 
>>
>>__
>>Best Regards,
>>Wang Kebo
>>
>>HomePage: http://www.huihoo.org/~mep
>>Blog: http://sharing.blogbus.com
>> 
>>_______________________________________________
>>python-chinese list
>>python-chinese at lists.python.cn
>>http://python.cn/mailman/listinfo/python-chinese
>>
>>
>>
>>    
>>
>
>
>  
>
Zeuux © 2025
京ICP备05028076号