【问题标题】:How to make a function equal to a variable? [duplicate]如何使函数等于变量? [复制]
【发布时间】:2021-03-09 08:14:22
【问题描述】:

在这段代码中,我得到了一个存在于基本路径中的路径,它正确地获取了路径,但问题是我想将该函数保存在另一个名为 hi 的变量中,但每当我打印它时,它给了我结果None

import os
def contain(dirname, dPath):
    dirfiles = os.listdir(dirname)

    fullpaths = map(lambda name: os.path.join(dirname, name), dirfiles)
    dirs = []

    for file in fullpaths:
        if os.path.isdir(file): dirs.append(file)

    folder = list(dirs)
    watching = [s for s in folder if dPath in s]
    copy = str(watching)[2:-2]
    print(copy)

def hello():
    base = '/home/bilal/Videos/folder1'
    destination = '/home/bilal/Videos/folder1/fd'
    hi = contain(base, destination)
    print(f"This is another time: {hi}")
    
hello()

【问题讨论】:

    标签: python


    【解决方案1】:

    应该是这样的:

    import os
    def contain(dirname, dPath):
        dirfiles = os.listdir(dirname)
    
        fullpaths = map(lambda name: os.path.join(dirname, name), dirfiles)
        dirs = []
    
        for file in fullpaths:
            if os.path.isdir(file): dirs.append(file)
    
        folder = list(dirs)
        watching = [s for s in folder if dPath in s]
        copy = str(watching)[2:-2]
        return copy#you need to return a value
    
    
    def hello():
        base = '/home/bilal/Videos/folder1'
        destination = '/home/bilal/Videos/folder1/fd'
        hi = contain(base, destination)
        print(f"This is another time: {hi}")
        
    hello()
    

    【讨论】:

      猜你喜欢
      • 2014-01-15
      • 2012-03-17
      • 2015-12-16
      • 2015-12-22
      • 1970-01-01
      • 2014-10-22
      • 2022-11-24
      • 2013-12-06
      • 2017-07-11
      相关资源
      最近更新 更多