尝试在Python中将一种对象类型转换为另一种对象类型 [英] Trying to cast one object type into another in Python

查看:159
本文介绍了尝试在Python中将一种对象类型转换为另一种对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

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 );

它将一个ON_NurbsCurve对象强制转换为ON_Curve对象.我不太确定这是否可能在Python中实现.我知道我可以将一个字符串并将其转换为一个整数,例如:int("1").我不确定使用其他未内置的对象类型的正确方法是什么.

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.

谢谢

推荐答案

您无法在Python中完全转换对象,通常也不需要这样做,因为Python没有强大的类型检查功能.

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

从技术上讲,铸造将现有对象的数据解释为另一种类型,本质上将对象视为正在铸造为新形状的液态金属(该术语的起源).在Python中,您可以做的是尝试将对象转换为另一种格式.通常,可以将另一个对象作为输入的对象将在其构造函数中接受它(例如,int()可以接受字符串和数字,并且如果存在这样的方法,则将在其他类型上调用__int__(),以让其他类型定义如何他们被转换).如果某些类型的主构造函数不能接受给定类型的对象,则某些类型甚至具有备用构造函数(例如,XML解析器可能在其主构造函数中接受文件名,并且具有from_string()from_file()类方法,它们可以接受字符串和文件状对象).

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).

在您给出的示例中,将一种类型的Curve对象转换为另一种类型,在Python中,您甚至可能不需要执行任何转换. NurbsCurve对象可能支持Curve的方法和属性,即使它不是严格的子类,任何接受Curve的函数都希望看到.如果它是严格的子类,那么绝对没有问题,也不需要转换!

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不会检查参数类型,除非有明确的代码可以这样做,而且大多数函数都没有这样的代码.相反,他们只是假设调用者不是傻瓜,并且传入了可以使用的对象.这通常称为鸭子打字".

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."

如果给定的函数不接受要传递的对象,则可以编写一个转换函数,一个多重继承子类或一个包装器类,以使您的行为足够类似于所需要的类型工作功能.但这通常是不需要的,因为设计API的人不是白痴,并且在可能的情况下会慷慨地接受.

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.

这篇关于尝试在Python中将一种对象类型转换为另一种对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆