无法从另一个类python访问变量 [英] Can't access variable from another class python
本文介绍了无法从另一个类python访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码,应该可以从设置"类中打印出self.cake和self.age.
Here is my code which is supposed to print self.cake and self.age from the class Settings.
from startup import Settings
class Profile(Settings):
def __init__(self):
super(Profile, self).__init__()
def print_settings(self):
print self.cake
print self.age
p = Profile()
p. print_settings()
其他python脚本
class Settings(object):
def __init__(self):
self.cake = 1
def number(self):
self.age = 5
但我不断得到:
AttributeError: 'Profile' object has no attribute 'age'
我需要能够从print_settings函数中打印变量.
I need to be able to print the variables from the print_settings function.
我该怎么办?
推荐答案
在调用 print_settings
方法之前,您必须设置 age
属性.
You have to set the age
attribute before calling the print_settings
method.
一个选择是:
p = Profile()
p.number()
p.print_settings()
这篇关于无法从另一个类python访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文