简体   繁体   English

关于在 Python 中创建自己的 complex() function 的建议?

[英]Suggestions on creating own complex() function in Python?

I'm working on a project in which I am required to self-create functions for converting numbers to complex, meaning I can not use complex().我正在做一个项目,在该项目中,我需要自行创建将数字转换为复数的函数,这意味着我不能使用 complex()。 Does anyone have any suggestions?有没有人有什么建议? Hitting a bit of a brick wall.撞到了一点砖墙。

Types in python are just classes. python 中的类型只是类。 You can therefore create a new type by making a class.因此,您可以通过制作 class 来创建新类型。 You can create a custom class Complex which has properties you need by writing magic methods like __add__ __gt__ etc. Have a look at python's "magic methods" for more info.您可以创建一个自定义的 class Complex ,它具有您需要的属性,方法是编写__add__ __gt__等魔术方法。查看 python 的“魔术方法”以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM