如何将一个功能的小计分配给另一个功能?

Posted

技术标签:

【中文标题】如何将一个功能的小计分配给另一个功能?【英文标题】:How to assign subtotal from one function to another function? 【发布时间】:2021-05-16 19:50:02 【问题描述】:

我必须将total_cost 变量设置为等于小计变量或来自产品类的变量。但是,当我尝试从类中获取值时。它给了我AttributeError: 'Product' object has no attribute '_Product__total_price' 的属性错误。我正在实施您应用促销代码的地方,然后它将从购物车中扣除总价。

这是init.py 和使用的函数。

@app.route('/applyPromo/<string:id>/', methods=['GET','POST'])
def apply_promo(id):
    promotions_dict = 
    db = shelve.open('promotions.db','w')
    promotions_dict = db['Promotions']
    total_cost = 100
    hidden = True
    applied = True
    click = True
    get_from_class = False
    print(promotions_dict)
    promotions = promotions_dict.get(UUID(id))
    db = shelve.open('storage.db', 'r')
    product_dict = db['products']
    for key in product_dict:
        product = product_dict[key]
        total_cost = product.get_total_price()
    print(total_cost)


    #when user apply promo, promo will get deleted from the list/dictionary of promos they have.
    if promotions["type"] == 1:
        total_cost = total_cost - 10
        hidden = False
        print(f"Total Cost : total_cost")
        promotions_dict.pop(UUID(id))

    elif promotions["type"] == 2:
        total_cost = total_cost - 5
        hidden = False
        print(f"Total Cost : total_cost")
        promotions_dict.pop(UUID(id))

    elif promotions["type"] == 3:
        total_cost = (70/100)*total_cost
        hidden = False
        print(f"Total Cost : total_cost")
        promotions_dict.pop(UUID(id))

    elif promotions["type"] == 4:
        total_cost = (80/100)*total_cost
        hidden = False
        print(f"Total Cost : total_cost")
        promotions_dict.pop(UUID(id))

    elif promotions["type"] == 5:
        total_cost = (85/100)*total_cost
        hidden = False
        print(f"Total Cost : total_cost")
        promotions_dict.pop(UUID(id))

    else:
        total_cost = (90/100)*total_cost
        hidden = False
        print(f"Total Cost : total_cost")
        promotions_dict.pop(UUID(id))

    db['Promotions'] = promotions_dict
    db.close()
    db.close()
    print(promotions_dict)


    session['promotion_applied'] = promotions["id"]

    return render_template("generatePromo.html", total_cost=total_cost,applied=applied, promotions_dict=promotions_dict,hidden=hidden,promotions=promotions,get_from_class=get_from_class, click=click)

@app.route('/shopping_cart')
def shopping_cart():
    # session.clear()
    error = None
    cart_items = []
    quantity_list = []
    subtotal = 0
    db = shelve.open('storage.db', 'r')
    product_dict = db['products']
    db.close()

    for products in session:
        item = product_dict.get(products)
        cart_items.append(item)

        if None in cart_items:
            cart_items.remove(None)

        quantity_list.append(session[products])

        if products in quantity_list:
            quantity_list.remove(products)

    for i in range(len(cart_items)):
        cart_items[i].set_purchased_quantity(quantity_list[i])
        # set total price for single item
        item_total = int(cart_items[i].get_price()) * int(cart_items[i].get_purchased_quantity())
        cart_items[i].set_total_price(item_total)
        # set total price of all items in cart
        subtotal += item_total

    print('QTY LIST', quantity_list)
    print('CART', cart_items)

    if not cart_items:
        error = "Cart Is Empty"

    return render_template('shoppingcart.html', products_list=cart_items, error=error, subtotal=subtotal)

这是产品类别。

from uuid import uuid4

class Product:
    def __init__(self, name, price, quantity, color, vase, remarks):
        self.__product__id = str(uuid4())
        self.__name = name
        self.__price = price
        self.__quantity = quantity
        self.__color = color
        self.__vase = vase
        self.__remarks = remarks
        self.__purchased_quantity = 0
        self.__total_price = 0

    def get_product_id(self):
        return self.__product__id

    def get_name(self):
        return self.__name

    def get_price(self):
        return self.__price

    def get_quantity(self):
        return self.__quantity

    def get_color(self):
        return self.__color

    def get_vase(self):
        return self.__vase

    def get_remarks(self):
        return self.__remarks

    def get_image(self):
        return self.__image

    def get_purchased_quantity(self):
        return self.__purchased_quantity

    def get_total_price(self):
        return self.__total_price

    def set_name(self, name):
        self.__name = name

    def set_price(self, price):
        self.__price = price

    def set_quantity(self, quantity):
        self.__quantity = quantity

    def set_color(self, color):
        self.__color = color

    def set_vase(self, vase):
        self.__vase = vase

    def set_remarks(self, remarks):
        self.__remarks = remarks

    def set_image(self, image):
        self.__image = image

    def set_purchased_quantity(self, purchased_quantity):
        self.__purchased_quantity = purchased_quantity

    def set_total_price(self, total_price):
        self.__total_price = total_price

这是错误的回溯。

Traceback

【问题讨论】:

【参考方案1】:

不应使用前导双下划线,因为您使用它们来定义类的属性 - 单下划线是常规的。使用前导双下划线的问题在于解释器“破坏”了这些属性的名称以避免继承的微妙问题,但我相信你正在看到导致问题。这是good read on the subject。

【讨论】:

我将 self.__total_price 更改为 self._total_price 但同样的问题仍然存在。我该怎么办? 呃哦,这对我的理论来说是个坏消息。 ;^) 能否包含错误发生时得到的回溯输出,以便我们查看导致错误的代码行? 我在帖子中包含了回溯输出。请务必看一下:) 回溯仍然显示带有前导双下划线的代码:self.__total_price 这是在您进行更改之后吗? 起初我将下划线从双下划线改为单下划线,但仍然出现同样的问题,所以我重做双下划线

以上是关于如何将一个功能的小计分配给另一个功能?的主要内容,如果未能解决你的问题,请参考以下文章

在 Excel 中将 Countif 添加到数组公式(小计)

查询报表增加小计功能

如何从数据透视表访问小计?

abap alv 不同字段 分类汇总

三日小计

Woocommerce 从结帐小计中排除税款