打印类中的所有变量? - Python
我正在制作一个可以访问存储在类中的数据的程序。例如,我有这个类:
#!/usr/bin/env python
import shelve
cur_dir = '.'
class Person:
def __init__(self, name, score, age=None, yrclass=10):
self.name = name
self.firstname = name.split()[0]
try:
self.lastname = name.split()[1]
except:
self.lastname = None
self.score = score
self.age = age
self.yrclass = yrclass
def yrup(self):
self.age += 1
self.yrclass += 1
if __name__ == "__main__":
db = shelve.open('people.dat')
db['han'] = Person('Han Solo', 100, 37)
db['luke'] = Person('Luke Skywalker', 83, 26)
db['chewbacca'] = Person('Chewbacca', 100, 90901)
所以使用这个我可以调用一个变量,例如:
print db['luke'].name
但如果我想打印所有变量,我就有点迷失了。
如果我运行:
f = db['han']
dir(f)
我得到:
['__doc__', '__init__', '__module__', 'age', 'firstname', 'lastname', 'name', 'score', 'yrclass', 'yrup']
但我希望能够打印这些的实际数据。
我该怎么做?
提前致谢!
I'm making a program that can access data stored inside a class. So for example I have this class:
#!/usr/bin/env python
import shelve
cur_dir = '.'
class Person:
def __init__(self, name, score, age=None, yrclass=10):
self.name = name
self.firstname = name.split()[0]
try:
self.lastname = name.split()[1]
except:
self.lastname = None
self.score = score
self.age = age
self.yrclass = yrclass
def yrup(self):
self.age += 1
self.yrclass += 1
if __name__ == "__main__":
db = shelve.open('people.dat')
db['han'] = Person('Han Solo', 100, 37)
db['luke'] = Person('Luke Skywalker', 83, 26)
db['chewbacca'] = Person('Chewbacca', 100, 90901)
So using this I can call out a single variable like:
print db['luke'].name
But if I wanted to print all variables, I'm a little lost.
If I run:
f = db['han']
dir(f)
I get:
['__doc__', '__init__', '__module__', 'age', 'firstname', 'lastname', 'name', 'score', 'yrclass', 'yrup']
But I want to be able to print the actual data of those.
How can I do this?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
而不是使用魔术方法,vars 可能更可取。
Rather than using magic methods, vars could be more preferable.
这将打印所有带有初始化值的类变量。
This will print all the class variables with values initialised.
定义 __str__ 或 __repr__ 方法并打印对象。
Define __str__ or __repr__ methods in your Person class and print the object.
尝试 beeprint
只要在
pp(db['han'])
之后 ,它就会打印:没有方法,没有私有属性。
Just try beeprint
after
pp(db['han'])
, it will print this:no methods, no private properties.
如果您想要一个方法来打印所有属性,您可以使用:
然后您需要调用该方法:
它会打印:
请注意,您可以使用值 {0:10} 来更改列的间距。
If you want a method to print all the attribute, you can use:
You then need to call the method:
and it will print:
Please note you can play around with the value {0:10} to change the spacing for the columns.
打印(变量(自身))
或者
pprint(vars(self))
并访问
self.variable
名称例如。
self._own_name
print(vars(self))
or
pprint(vars(self))
and to access
self.variable
nameEg.
self._own_name
如果您只需要定义的变量的值,您可以这样做:
您不会打印函数或内部 Python 变量(以
__
开头和结尾),称为 dunders。If you want only the values of the variables you defined, you can do:
You will not print functions or internal Python variables (start and end with
__
), called dunders.