考虑一下这个颜色解析器的玩具示例。它解析用户输入的颜色,然后描述该颜色。对于此玩具示例,描述颜色意味着仅返回字符串。无论如何,这仅适用于一种颜色,但不适用于多种颜色。
class Colorparser():
def describe_color(self, val):
"""Describe a color """
color = parse_color(val)
desc = "A description" # The real script does some crazy stuff here ;)
return desc
def describe_palette(self, vals):
"""Describe colors in palette"""
descs = [describe_color(v) for v in vals]
return descs
def parse_color(self, val):
"""Returns an RGB tupple if color is RGB,
and converts a hex color to RGB otherwise """
if type(val) == str:
color = hex_to_rgb(val)
elif type(val) == list:
color = val
return color
def hex_to_rgb(self, color):
"""Convertes a hex color to rgb"""
value = color.lstrip('#')
lv = len(value)
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
现在这是我的程序:
c = Colorparser()
color = "#F7F7F7"
colors = ["#F7F7F7", "#F7F7F7"]
c.describe_color(color) # works
c.describe_palette(colors) # does not work
我得到了错误:
NameError Traceback (most recent call last)
<ipython-input-77-4264da0e7d08> in <module>()
40
41 c.describe_color(color) # works
---> 42 c.describe_palette(colors) # does not work
1 frames
<ipython-input-77-4264da0e7d08> in <listcomp>(.0)
13 Describe colors in palette
14 """
---> 15 descs = [describe_color(v) for v in vals]
16 return descs
17 def hex_to_rgb(self, color):
NameError: name 'describe_color' is not defined
有人知道为什么describe_color(color)
有效但describe_palette(colors)
不有效吗?
答案 0 :(得分:0)
您必须从describe_palette
方法返回值。
另外,您必须使用self.
这是更正的程序:
class Colorparser:
def describe_color(self, val):
"""Describe a color """
color = self.parse_color(val)
desc = "A description" # The real script does some crazy stuff here ;)
return desc
def describe_palette(self, vals):
"""Describe colors in palette"""
descs = [self.describe_color(v) for v in vals]
return descs
def parse_color(self, val):
"""Returns an RGB tupple if color is RGB,
and converts a hex color to RGB otherwise """
if type(val) == str:
color = self.hex_to_rgb(val)
elif type(val) == list:
color = val
return color
def hex_to_rgb(self, color):
"""Convertes a hex color to rgb"""
value = color.lstrip('#')
lv = len(value)
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
c = Colorparser()
color = "#F7F7F7"
colors = ["#F7F7F7", "#F7F7F7"]
c.describe_color(color)
c.describe_palette(colors)
答案 1 :(得分:0)
Python需要引用与“ self”相同的对象。以下应该起作用:
class Colorparser():
def describe_color(self, val):
"""Describe a color """
color = parse_color(val)
desc = "A description" # The real script does some crazy stuff here ;)
return desc
def describe_palette(self, vals):
"""Describe colors in palette"""
descs = [self.describe_color(v) for v in vals] # <-- change in this line
return descs
def parse_color(self, val):
"""Returns an RGB tupple if color is RGB,
and converts a hex color to RGB otherwise """
if type(val) == str:
color = hex_to_rgb(val)
elif type(val) == list:
color = val
return color
def hex_to_rgb(self, color):
"""Convertes a hex color to rgb"""
value = color.lstrip('#')
lv = len(value)
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
我认为您从“ describe_color”进行的“ parse_color”调用也需要“ self”。字首。