python函数式编程之(open(var1 if not var2 else var2))

真真搞不懂,python怎么会这样!

下面函数的open语句中的意思:

若是传入的参数为空的话,就返回file_path,否则就返回file_name.

初学python,这个理解起来的确有意思!

关于with as的语句理解,在ibm的developwork中有个廖雪峰的文章写得不错。我已经转了奋斗

def get_sm_ip(file_name=""):

    """
    :param file_name:

    :return:

    """
    try:

            file_path = os.path.join("/tmp", "readme.log")

           <strong> with open(file_path if not file_name else file_name) as f:</strong>
                try:
                    contents = pickle.load(f)
                    root_element = ET.fromstring(contents)
                    i = root_element.find("Config").find("RemoteAddress").text
                except Exception as e:
                    print e
                    pass
    except Exception as e:

        print e
        pass

    return i

猜你喜欢

转载自blog.csdn.net/u011627161/article/details/51106220