Как создать конструктор, который будет получать параметры разных типов?Python

Программы на Python
Anonymous
Как создать конструктор, который будет получать параметры разных типов?

Сообщение Anonymous »

У меня есть класс Point. Я хочу, чтобы он мог получать параметры типа double и Some Type.

Код: Выделить всё

Point.pxd:
from libcpp.memory cimport shared_ptr, weak_ptr, make_shared
from SomeType cimport _SomeType, SomeType

cdef extern from "Point.h":
cdef cppclass _Point:
_Point(shared_ptr[double] x, shared_ptr[double] y)
_Point(shared_ptr[double] x, shared_ptr[double] y, shared_ptr[double] z)
_Point(shared_ptr[_SomeType] x, shared_ptr[_SomeType] y)
_Point(shared_ptr[_SomeType] x, shared_ptr[_SomeType] y, shared_ptr[_SomeType] z)

shared_ptr[_SomeType] get_x()
shared_ptr[_SomeType] get_y()
shared_ptr[_SomeType] get_z()

cdef class Point:
cdef shared_ptr[_Point] c_point

Код: Выделить всё

Point.pyx:
from Point cimport *

cdef class Point:
def __cinit__(self, SomeType x=SomeType("0", None), SomeType y=SomeType("0", None), SomeType z=SomeType("0", None)):
self.c_point = make_shared[_Point](x.thisptr, y.thisptr, z.thisptr)

def __dealloc(self):
self.c_point.reset()

def get_x(self) -> SomeType:
cdef shared_ptr[_SomeType] result = self.c_point.get().get_x()
cdef SomeType coord = SomeType("", None, make_with_pointer = True)
coord.thisptr = result
return coord

def get_y(self) -> SomeType:
cdef shared_ptr[_SomeType] result = self.c_point.get().get_y()
cdef SomeType coord = SomeType("", None, make_with_pointer = True)
coord.thisptr = result
return coord

def get_z(self) -> SomeType:
cdef shared_ptr[_SomeType] result = self.c_point.get().get_z()
cdef SomeType coord = SomeType("", None, make_with_pointer = True)
coord.thisptr = result
return coord

property x:
def __get__(self):
return self.get_x()

property y:
def __get__(self):
return self.get_y()

property z:
def __get__(self):
return self.get_z()
Как мне написать файлы .pxd и .pyx, чтобы мой конструктор Point мог получать параметры другого типа?

Подробнее здесь: https://stackoverflow.com/questions/784 ... parameters

Вернуться в «Python»