python对象类型转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4529070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python object typecasting
提问by Hyman Wong
Ive been trying search how to pass object reference in python and type cast it similar to Java but no to avail. I duno if this topic exists somewhere here.
我一直在尝试搜索如何在 python 中传递对象引用并将其类型转换为类似于 Java 但无济于事。我不知道这个话题是否存在于此。
My trouble is i have to pass the object reference to a class constructor. But i duno how to typecast the reference to an object. In java though I have accomplish this but i have to transfer the code to the server side.
我的麻烦是我必须将对象引用传递给类构造函数。但我不知道如何对对象的引用进行类型转换。在java中,虽然我已经完成了这一点,但我必须将代码传输到服务器端。
many thanks, Hyman
非常感谢,Hyman
class SearchRectangle:
def __init__(self, lower_left_subgrid_x, lower_left_subgrid_y, rectangle_width, rectangle_height):
self.min_subgrid_x = int(lower_left_subgrid_x)
self.max_subgrid_x = int(self.min_subgrid_x + rectangle_width -1)
self.min_subgrid_y = int(lower_left_subgrid_y)
self.max_subgrid_y = int(self.min_subgrid_y + rectangle_height -1)
...blah
class SearchRectangleMultiGrid:
# parent rectangle should be a SearchRectangle instance
def __init__(self, parent_rectangle):
self.parent_rectangle = SearchRectangle()parent_rectangle
# test codes
test_rect = SearchRectangle(test_subgrid.subgrid_x, test_subgrid.subgrid_y, 18, 18)
print "\n\nTest SearchRectangle";
print test_rect.to_string()
print test_rect.sql_clause
test_rec_multi = SearchRectangleMultiGrid(test_rect)
print "\n\nTest SearchRectangleMulti"
test_rec_multi.parent_rectangle.to_string()
回答by Falmarri
There's no reason to cast anything in python. What are you trying to do? Just use the object like you would, and if it's not of the correct type it will fail. There's no such thing as casting since variable names don't have a type associated with them.
没有理由在 python 中投射任何东西。你想做什么?只需像您一样使用该对象,如果它的类型不正确,它将失败。没有强制转换之类的东西,因为变量名没有与之关联的类型。
回答by Wolph
Python is a dynamically typed language and as such, it doesn't make much sense to cast something unless you specifically need it in that type.
Python 是一种动态类型语言,因此,除非您在该类型中特别需要它,否则转换某些内容没有多大意义。
In Python you should use Duck Typing instead: http://en.wikipedia.org/wiki/Duck_typing
在 Python 中,您应该改用 Duck Typing:http: //en.wikipedia.org/wiki/Duck_typing
So instead of trying to convert parent_rectangleto a SearchRectangle()you should simply test if SearchRectangle()has the properties you need.
因此,与其尝试转换parent_rectangle为 a,SearchRectangle()您应该简单地测试是否SearchRectangle()具有您需要的属性。
Or if you really want to be sure that you'll always get a SearchRectangle(), use isinstancelike this:
或者,如果你真的想确保你总是得到一个SearchRectangle(),请isinstance像这样使用:
if isinstance(parent_rectangle, SearchRectangle):
This might be a good read for you: http://dirtsimple.org/2004/12/python-is-not-java.html
这对你来说可能是一个很好的阅读:http: //dirtsimple.org/2004/12/python-is-not-java.html
回答by Lennart Regebro
Further explanation:
进一步解释:
Castingis the act of taking a pointer/reference to one type of object, and say to the compiler "Yeah, I know this is a foo reference but please pretend it is a bar reference".
Casting是将指针/引用指向一种类型的对象,然后对编译器说“是的,我知道这是一个 foo 引用,但请假装它是一个 bar 引用”。
Python do not have pointers/references in that sense (although in another sense, everything is references). Also, the compiler/interpreter doesn't care what the type is in the first place. Hence, casting is both impossible and pointless.
Python 在这个意义上没有指针/引用(尽管在另一种意义上,一切都是引用)。此外,编译器/解释器一开始并不关心类型是什么。因此,铸造既不可能又毫无意义。
So in your example: Just skip the type casting. It'll work anyway. And if it doesn't. then make a question on that problem.
所以在你的例子中:只需跳过类型转换。无论如何它会起作用。如果没有。然后就那个问题提出问题。

