[英]Python 3: Calling a class function inside of __init__
I have a little question about python 3.我有一个关于python 3的小问题。
I want to create a class, which is using a function from within of that class.我想创建一个类,它使用该类中的函数。 Just like:就像:
class Plus:
def __init__(self, x, y):
self.x = x
self.y = y
self.test()
def test(self):
return self.x + self.y
now I am doing something like现在我正在做类似的事情
a = Plus(5,6)
print(a)
and python is giving me蟒蛇给了我
<__main__.Plus object at 0x000000000295F748>
and not 11 as I want it.而不是我想要的 11。 I know that I can get 11 by我知道我可以得到 11
a = Plus(5, 6).test()
print(a)
but that's not what I want.但这不是我想要的。 I want to call the class and getting the result without adding .test() to it.我想在不添加 .test() 的情况下调用该类并获取结果。
Can you help me?你能帮助我吗?
I would go for:我会去:
class Plus:
def __init__(self, x, y):
self.x = x
self.y = y
self.test()
def test(self):
res = self.x + self.y
self.__repr__ = lambda:str(res)
return res
>>> Plus(5,5)
10
>>> a = Plus(5,5)
>>> a
10
>>> a.test()
10
This way you are not recomputing the sum each time you call print, its updated when you call the test method.这样你就不会在每次调用 print 时重新计算总和,当你调用测试方法时它会更新。
You'd need to define a __str__
method for your Plus
class:你需要为你的Plus
类定义一个__str__
方法:
class Plus:
def __init__(self, x, y):
self.x = x
self.y = y
def test(self):
return self.x + self.y
def __str__(self):
return str(self.test())
now I am doing something like现在我正在做类似的事情
a = Plus(5,6)
print(a)
and python is giving me蟒蛇给了我
<__main__.Plus object at 0x000000000295F748>
and not 11 as I want it.而不是我想要的 11。 I know that I can get 11 by我知道我可以得到 11
a = Plus(5, 6).test()
print(a)
but that's not what I want.但这不是我想要的。 I want to call the class and getting the result without adding .test() to it.我想在不添加 .test() 的情况下调用该类并获取结果。
I am not sure what do you mean by 'and not 11 as I want it'.我不确定您所说的“而不是我想要的 11”是什么意思。 If you want Plus(5, 6)
to actually return 11
( int
instance), you should make Plus
a function that returns the sum.如果您希望Plus(5, 6)
实际上返回11
( int
实例),您应该使Plus
成为一个返回总和的函数。 Alternatively you can override __new__
method and hook upon object creation -- but this is a bad idea.或者,您可以覆盖__new__
方法并挂钩对象创建——但这是一个坏主意。
What are you trying to achieve?你想达到什么目的?
I doubt, that by 'and not 11 as I want it' you want something special to be printed (formatted, represented).我怀疑,通过“而不是我想要的 11”,您是否想要打印一些特别的东西(格式化、表示)。 If so, override __str__
or __unicode__
or __repr__
method.如果是这样,覆盖__str__
或__unicode__
或__repr__
方法。
Edit: ignore this answer, it is a comment on a misinterpretation of the question编辑:忽略此答案,这是对问题的误解的评论
This is just wrong.这是错误的。 when you instantiate an object, you'd expect to get a reference to that object.当您实例化一个对象时,您希望获得对该对象的引用。
if you just want a global function returning a number, why even bother to make a class with an init?如果你只是想要一个返回一个数字的全局函数,为什么还要费心用一个 init 来创建一个类呢?
in python you shouldn't want static class's like in C# for encapsulation.在 python 中,您不应该像 C# 中那样使用静态类进行封装。 instead name the module something, and use that for encapsulation.而是为模块命名,并将其用于封装。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.