NameError:名称“”未定义

如何解决NameError:名称“”未定义

我下面的代码应该将客户对象的 describe 方法的返回值打印到终端......但它没有。问题似乎是 NameError: name 'price' 未定义。

class TicketMixin:
    
    """ Mixin to calculate ticket price based on age """
    def calculate_ticket_price(self,age):
        
        ticket_price = 0
        price = ticket_price
        
        if self.age < 12:
            price = ticket_price + 0
        elif self.age < 18:
            price = ticket_price + 15
        elif self.age < 60:
            price = ticket_price + 20
        elif self.age >= 60:
            price = ticket_price + 10
        return price

class Customer(TicketMixin):
    """ Create instance of Customer """
    def __init__(self,name,age,):
        self.name = name
        self.age = age
    
    def describe(self):
        return f"{self.name} age {self.age} ticket price is {price}"
        
customer = Customer("Ryan Phillips",22)
print(customer.describe())

有人能告诉我我错过了什么吗?

解决方法

您没有拨打 calculate_ticket_price

def describe(self):
    return f"{self.name} age {self.age} ticket price is {self.calculate_ticket_price(self.age)}"

请注意,calculate_ticket_price 可以或者接受一个 age 参数,在这种情况下,它不需要假设 self.age 存在:

class TicketMixin:
    
    """ Mixin to calculate ticket price based on age """
    def calculate_ticket_price(self,age):
        
        ticket_price = 0
        price = ticket_price
        
        if age < 12:
            price = ticket_price + 0
        elif age < 18:
            price = ticket_price + 15
        elif age < 60:
            price = ticket_price + 20
        elif age >= 60:
            price = ticket_price + 10
        return price

或者你可以做出这个假设并完全去掉 age 参数:

class TicketMixin:
    
    """ Mixin to calculate ticket price based on age """
    def calculate_ticket_price(self):
        
        ticket_price = 0
        price = ticket_price
        
        if self.age < 12:
            price = ticket_price + 0
        elif self.age < 18:
            price = ticket_price + 15
        elif self.age < 60:
            price = ticket_price + 20
        elif self.age >= 60:
            price = ticket_price + 10
        return price

那么 describe 的主体将简单地省略任何显式参数:

def describe(self):
    return f"{self.name} age {self.age} ticket price is {self.calculate_ticket_price()}"

在前者中,请注意在定义中不要使用 self ;这表明它应该是一个静态方法,或者只是一个可以被 Customer 使用而无需任何混合类的常规函数​​。

,

切普纳是对的。您必须在 init 函数中调用 calculate_ticket_price。正确的语法是使用 super 关键字。

示例:

class Customer(TicketMixin):
    """ Create instance of Customer """
    def __init__(self,name,age,):
        self.name = name
        self.age = age
        self.price = super().calculate_ticket_price(self.age)
    
    def describe(self):
        return f"{self.name} age {self.age} ticket price is {self.price}"

您可以通过点击 here 了解有关 super 关键字的更多信息。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?