Python:返回无效,但打印有效 [英] Python: Return doesn't work but Print works

查看:68
本文介绍了Python:返回无效,但打印有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建的库的目的是在输入颜色名称时返回颜色的十六进制值.

The point of the library I'm creating is to return the Hexadecimal value of a colour when you enter the name of the colour.

上面的程序可以很好地与print一起使用,尽管它不会在将 print 替换为 return 后立即返回值.但是返回值的全部要点已经消失了,因为它不能与其他程序一起使用.return(#F2F3F4")不起作用

The above program works fine with print although it doesn't return a value as soon as print is replaced with return. But the entire point of returning a value is gone as it can not be used in conjunction with other programs. return("#F2F3F4") doesn't work

是的,我在没有括号的情况下尝试了,并且没有任何区别.希望您能解决问题.预先谢谢你!

And yes I tried it without the brackets and it doesn't make any difference. Hope you can figure out the problem. Thank you in advance!

class ColourConst():
    def __init__(self, colour):
        col = ""
        #Shades of White
        def Anti_flash_white():
             print("#F2F3F4")

        def Antique_white():
            print("#FAEBD7")

        def Beige():
            print("#F5F5DC")

        def Blond():
            print("#FAF0BE")

        ColourCon = {
        #Shades of White
        "Anti-flash white": Anti_flash_white, 
        "Antique white": Antique_white,
        "Beige": Beige,
        "Blond" : Blond
        }
        myfunc = ColourCon[colour]
        myfunc()

ColourConst("Anti-flash white")

推荐答案

如果您使用 return ,它返回一个值,但是除非您也使用 print,它不会打印它.

It does return a value if you use return, but unless you also use print, it won't print it.

class ColourConst():
    def __init__(self, colour):
        def Anti_flash_white():
            return "#F2F3F4" # return here
        def Antique_white():
            return "#FAEBD7" # and here
        def Beige():
            return "#F5F5DC" # and here
        def Blond():
            return "#FAF0BE" # you get the point...
        ColourCon = {
            "Anti-flash white": Anti_flash_white, 
            "Antique white": Antique_white,
            "Beige": Beige,
            "Blond" : Blond
        }
        myfunc = ColourCon[colour]
        print(myfunc()) # add print here

ColourConst("Anti-flash white")

话虽如此,这是一种非常可怕的方式.首先,这是一个类的构造函数,根据定义,该构造函数只能返回该类的新创建实例 self .取而代之的是,您可以使它成为一个返回值的函数,并在调用该函数时打印该值,从而使其具有更高的可重用性.另外,除了将颜色名称映射到函数(每个返回值)之外,您还可以直接将名称映射到值.

Having said that, this is a pretty horrible way of doing this. First, this is the constructor of a class, which by definition can only return the newly created instance of that class, self. Instead, you can just make it a function returning the value, and print the value when you call the function, making it much more reusable. Also, instead of mapping color names to functions, each returning the value, you can just map names to values directly.

def colour_const(name):
    colour_codes = {
        "Anti-flash white": "#F2F3F4", 
        "Antique white": "#FAEBD7",
        "Beige": "#F5F5DC",
        "Blond" : "#FAF0BE"
    }
    return colour_codes.get(name, "unknown color")

print(colour_const("Anti-flash white"))

这篇关于Python:返回无效,但打印有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆