python object类型转换,尝试在Python中将一种对象类型转换为另一种对象类型

在Python中,你不能像在某些其他语言中那样精确地进行类型转换,因为Python没有强类型检查。通常,你不需要转换对象,因为Python支持鸭子类型。如果一个函数接受某种类型的对象,即使传入的对象不是严格的子类,只要它支持所需的方法和属性,也能正常工作。只有在特定情况下,如API限制或需要特定行为时,才需要编写转换函数或使用多继承或包装类来使对象行为类似。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have this bit of code:

const ON_Curve* curve = ...;

const ON_NurbsCurve* nurb = ON_NurbsCurve::Cast( curve );

if( nurb )

{

ON_Ellipse ellipse;

double tolerance = model.m_settings.m_ModelUnitsAndTolerances.m_absolute_tolerance;

bool rc = nurb->IsEllipse( 0, &ellipse, tolerance );

It casts a ON_NurbsCurve object to ON_Curve object. I am not quite sure if that's even possible in Python. I know i can take a string and cast it into an integer like: int("1"). I am not sure what is the right way to do so with other object types that are not built in.

thank you

解决方案

You can't exactly cast objects in Python, nor do you generally need to because Python doesn't have strong type-checking.

Technically, casting is interpreting an existing object's data as if it were another type, essentially treating the object as a liquid metal that is being cast into a new shape (the origin of the term). In Python, what you can do is try to convert an object to another format. Usually an object that can take another object as input will accept it in its constructor (e.g. int() can take strings as well as numbers, and will call __int__() on other types if such a method exists, to let other types define how they are converted). Some types even have alternate constructors if their main constructor can't accept a given kind of object (for example, an XML parser might accept a filename in its main constructor, and have from_string() and from_file() class methods that accept strings and file-like objects, respectively).

In the example you give, of converting one type of Curve object into another, in Python you probably wouldn't even need to do any conversion. The NurbsCurve object probably supports the methods and attributes of Curve that any function that accepts a Curve would expect to see, even if it isn't a strict subclass. And if it is a strict subclass, then there's definitely no problem and no need to convert!

Python doesn't check argument types unless there is explicit code to do so, and most functions don't have such code. Instead, they just assume the caller is not a doofus and has passed in an object they can use. This is commonly called "duck typing."

If a given function doesn't accept the object you want to pass in, you could write a conversion function, a multiple-inheritance subclass, or a wrapper class to make what you have behave enough like the type that's needed to get the function to work. But this is usually not needed, because people who design APIs are not idiots and will be generous in what they accept when possible.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值