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

问题描述 投票:0回答:1

我试图在不使用为我完成所有工作的库的情况下制作一类 Polar 数字。我正在尝试制作 pow 功能。极坐标数被提升为 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>]

python python-3.x object generator
1个回答
0
投票

你列出了里面的极地物体

__pow__
然后你把它归还了。这正是你看到的
[<__main__.polar object at ...

所以唯一错误的是最后的打印语句。这有效:

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__
就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.