Python:类上的生成器问题[重复]

mnemlml8  于 2023-04-08  发布在  Python
关注(0)|答案(1)|浏览(132)

此问题已在此处有答案

What is the difference between str and repr?(28回答)
昨天关门了。
我想做一个Polar数的类,而不使用为我做所有工作的库。我想做一个 pow 函数。当Polar数被提升到0和1之间的幂时,你会期望多个结果,问题是我的代码表现得像一个生成器,我认为它不应该。

import math

def sin(x,/):
    return math.sin(math.radians(x))

def cos(x,/):
    return math.cos(math.radians(x))

def arctan(x,/):
    return math.degrees(math.atan(x))

class polar():
    def __init__(self, complex_num,angle=None):
        if angle != None:
            self.r = complex_num
            self.alpha = angle
        elif type(complex_num) in (float, int, complex):
            complex_num = complex(complex_num)
            self.r = ((complex_num.real**2)+(complex_num.imag**2))**0.5
            if complex_num.real != 0:
                if complex_num.imag > 0 and complex_num.real > 0:
                    self.alpha = arctan(complex_num.imag/complex_num.real)
                elif complex_num.imag > 0 and complex_num.real < 0:
                    self.alpha = 180-(arctan(complex_num.imag/abs(complex_num.real)))
                elif complex_num.imag < 0 and complex_num.real < 0:
                    self.alpha = 180+(arctan(abs(complex_num.imag)/abs(complex_num.real)))
                else:
                    self.alpha = 360-(arctan(abs(complex_num.imag)/complex_num.real))
            else:
                if complex_num.real > 0:
                    self.alpha = 90
                else:
                    self.alpha = 270
        while self.alpha >= 360:
            self.alpha -=360
        while self.alpha < 0:
            self.alpha += 360
            

    def __pow__(self,exponent):
        result = []
        if type(exponent) not in (int,float):
            raise TypeError("Exponent must be a number")
        if exponent == 0:
            return polar(1,0)
        elif exponent == 1:
            return self
        elif exponent > 1 or exponent < 0:
            return polar(self.r**exponent, self.alpha*exponent)
        else:
            index = int(1/exponent)
            for i in range(index):
                alpha = (self.alpha/index) + ((360/index)*i)
                result.append(polar(self.r**exponent, alpha))
            return result
                
            
            
    def __str__(self) -> str:
        return f"{self.r} {self.alpha}º"
    
    def complexToPolar(polar_num: str,/):
        try:
            polar_num = polar_num.replace("º","")
            polar_num = polar_num.split()
            r = float(polar_num[0])
            alpha = float(polar_num[1])
            z = r*(cos(alpha)+sin(alpha)*1j)
            return z
        except:
            raise Exception(TypeError)

print(polar(0-243j)**(1/5)

我尝试使用yield,但变量“i”根本没有递增,所以我可以得到第一个结果,但不是所有结果,预期结果是:[3.0 54.0º, 3.0 126.0º, 3.0 198.0º, 3.0 270.0º, 3.0 342.0º]但我得到的是:[<__main__.polar object at 0x00000198710D66D0>, <__main__.polar object at 0x00000198710D6690>, <__main__.polar object at 0x00000198710D68D0>, <__main__.polar object at 0x00000198710D65D0>, <__main__.polar object at 0x00000198710D6850>]

pgky5nke

pgky5nke1#

您在__pow__中创建了一个极坐标对象列表,然后返回它。
所以唯一的错误是你在结尾的print语句。这是可行的:

for obj in polar(0-243j)**(1/5):
    print(f"{obj.r} {obj.alpha}º")

输出:

3.0 54.0º
3.0 126.0º
3.0 198.0º
3.0 270.0º
3.0 342.0º

如果for _ in很难看到,那么在控制台上一次处理一个语句:

>>> mylist = polar(0-243j)**(1/5)
>>> mylist
[<__main__.polar obje...86F8DE8C0>, <__main__.polar obje...86F8DEA10>, <__main__.polar obje...86F8DE9B0>, <__main__.polar obje...86F8DE950>, <__main__.polar obje...86F8DE8F0>]
>>> mylist[0]
<__main__.polar object at 0x000001A86F8DE8C0>
>>> mylist[0].r
3.0
>>> mylist[0].alpha
54.0

编辑:好吧,我的眼睛跳过了你的__str__定义。@metatoaster的评论是正确的。只要用__repr__替换它就可以了。

相关问题