[英]How to execute 0 when printing out the loop? I've been trying to print out the factorial in python. I need 0 to be printed in the output
[英]How to print attributes in python. it is printing location. but i need to print ''25"
它应该打印一个数字,而不是位置。
a=25
class me:
def it(a):
print(a)
m=me()
m.it()
我应该得到 25,但我得到 - <__main__.me object at 0x7f98cb648b70>
这应该是类,必须在 class 内,我定义设置/获取方法
class me:
def __init__(self, a):
self.a = a
def set_a(self, v):
self.a = v
def get_a(self):
return self.a
m = me(5)
m.get_a()
m.set_a(12)
m.get_a()
Output
5
12
在您的情况下it
的方法被视为instance method
, instance method
的第一个参数始终是实例( self
)。 因此,当您执行print(a)
时,它会打印实例的位置,而不是打印变量a
的值。
您可以it
方法声明为 static 如下所示
a=25
class me:
@staticmethod
def it():
print(a)
m=me()
m.it()
或者你可以做
a=25
class me:
def it(self):
print(a)
m=me()
m.it()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.