更改实例变量的值

时间:2018-11-17 07:56:06

标签: python instance-variables

我想将名称打印为tanya,但是由于self.name = None已在构造函数中分配,因此它正在打印None。因此,在调用check函数时如何打印tanya:

class A:
    def __init__(self):
        self.name = None

    def price(self):
        self.name = "tanya"

    def check(self):
        print(self.price())

a=A()
a.check()

2 个答案:

答案 0 :(得分:1)

构造函数不是问题

http://(your ip like 192....)/your localhost project folder/image.jpg将打印print(self.price()),因为you are printing the result of the function

然后在打印后设置None,但您忽略了它

相反,我想你想要

self.name="tanya"

忘记检查功能

答案 1 :(得分:0)

add_action( 'woocommerce_cart_totals_before_order_total', 'bbloomer_wc_discount_total_30', 10, 1 );
add_action( 'woocommerce_review_order_before_order_total', 'bbloomer_wc_discount_total_30', 10, 1 ); 
function bbloomer_wc_discount_total_30() {


global $woocommerce;

$discount_total = 0;

foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values) {

$_product = $values['data'];

    if ( $_product->get_regular_price() ) {
    $regular_price = $_product->get_regular_price();
    $sale_price = $_product->get_sale_price();
    $discount = ($regular_price - $coupon) ;
    $discount_total += $discount;
    }

}

if ( $discount_total > 0 ) {
echo '<tr class="cart-discount">
<th>'. __( 'Razem netto', 'woocommerce' ) .'</th>
<td data-title=" '. __( 'Razem netto', 'woocommerce' ) .' ">'
. wc_price( $discount_total - $woocommerce->cart->discount_cart ) .'</td>
</tr>';
}

您只需要在class A: def __init__(self): self.name=None def price(self): self.name="tanya" return self.name def check(self): print(self.price()) a=A() a.check() 函数中添加return语句就可以了!