【问题标题】:How to get without using the return statement class inside function variable value?如何在不使用函数变量值的返回语句类的情况下获取?
【发布时间】:2021-12-29 12:02:29
【问题描述】:

这是我的代码。我什至不能使用数据库。

我需要在函数变量值中不使用 return 语句类。

class Hello:
    def alex_details():
        name = "Hello"
        last_nem = "World"

    def liam_details():
        name = "Hello"
        last_name = "World"


hi = Hello()

print(hi.alex_details.name)

错误:


print(hi.alex_details.name)
AttributeError: 'function' object has no attribute 'name'

【问题讨论】:

  • 有很多方法可以做到这一点,但目前,这两个函数中只有局部变量。避免返回语句的原因是什么?
  • 如果你想要命名空间使用嵌套类。
  • 我想使用它的不同功能和类

标签: python python-3.x


【解决方案1】:

也许这段代码可以帮助您解决问题。

class Hello:
    def __init__(self):
        self.name = "default name"
        self.last_name = "default last name"

    def alex_details(self):
        self.name = "alex"
        self.last_name = "sandra"

    def liam_details(self):
        self.name = "liam"
        self.last_name = "World"


hi = Hello()
print(hi.name) #output: "default name"
hi.alex_details()
print(hi.name) #output: "alex"
hi.liam_details()
print(hi.name) #output: "liam"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 2012-04-04
    • 2019-04-17
    • 1970-01-01
    • 2013-05-29
    • 2022-06-23
    • 1970-01-01
    相关资源
    最近更新 更多