python - 字符串格式化自定义对象

标签 python string-formatting python-3.6

我有一个可以使用的基本购物车。我正在尝试学习字符串格式 - 自定义对象:https://pyformat.info/#custom_1

我想做的是:

print("Your cart contains: {0:short}".format(cart))

应该返回:

Your cart contains: cucumbers, tissues, tomatoes, toothpaste

我的格式方法错误。我不断得到:

Traceback (most recent call last):
  File "C:/Users/Mark/PycharmProjects/main/main.py", line 54, in <module>
    print("Your cart contains: {0:short}".format(cart))
TypeError: __format__ must return a str, not NoneType

我的代码:

class Item(object):
    def __init__(self, unq_id, name, price, qty, measure):
        self.unq_id = unq_id
        self.product_name = name
        self.price = price
        self.qty = qty
        self.measure = measure


class Cart(object):
    def __init__(self):
        self.content = dict()

    def __format__(self, format_type):
        if format == 'short':
            return ', '.join(item.name for item in self.content)

    def add(self, item):
        if item.unq_id not in self.content:
            self.content.update({item.unq_id: item})
            return
        for k, v in self.content.get(item.unq_id).items():
            if k == 'unq_id':
                continue
            elif k == 'qty':
                total_qty = v.qty + item.qty
                if total_qty:
                    v.qty = total_qty
                    continue
                self.remove_item(k)
            else:
                v[k] = item[k]

    def get_total(self):
        return sum([v.price * v.qty for _, v in self.content.items()])

    def get_num_items(self):
        return sum([v.qty for _, v in self.content.items()])

    def remove_item(self, key):
        self.content.pop(key)


if __name__ == '__main__':
    cart = Cart()
    cart.add(Item(1, "Cucumbers", 1., 1, 'kg'))
    cart.add(Item(2, "Tissues", 1., 2, 'dozen'))
    cart.add(Item(3, "Tomatoes", 1., 5, 'box'))
    cart.add(Item(4, "Toothpaste", 1., 5, 'box'))
    # print("You have %i items in your cart for a total of $%.02f" % (cart.get_num_items(), cart.get_total()))
    print("Your cart contains: {0:short}".format(cart))
    # cart.remove_item(1)
    # print("You have %i items in your cart for a total of $%.02f" % (cart.get_num_items(), cart.get_total()))

最佳答案

问题出在您的 __format__ 实现中:

def __format__(self, format_type):
    if format == 'short':
        return ', '.join(item.name for item in self.content)
  • 问题 1:if format == 'short':
    -------------^--------------- - 应该是 format_type

  • 问题 2:...(item.name - Item 类上没有属性 name

<小时/>

正确的定义:

def __format__(self, format_type):
    if format_type == 'short':
        return ', '.join(i.product_name for i in self.content.values())
<小时/>

然后,行 print("Your cart contains: {0:short}".format(cart)) 将输出:

Your cart contains: Cucumbers, Tissues, Tomatoes, Toothpaste

关于python - 字符串格式化自定义对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46812643/

相关文章:

python - 当 python 脚本有未处理的异常时退出代码

python - 将字节字符串格式化为另一个字节字符串

python - 如何格式化小数以始终显示 2 个小数位?

python - uWSGI找不到所有Python安装中都存在的模块

python - 在 pybind11 (C++) 中使用内置的 "open"python 方法?

python - 使用一个文件运行良好但另一个文件未导入的位置访问我的目录中的文件(?)

javascript - 使用 Jinja2 设置输入字段的值

python - 用多种推导式填充字典

python - 为什么删除 except 中使用的变量

java - 限制 Java 中 println() 语句的字符数