Using python's super() within a new Julia-defined Python class #423
-
I need to define a Python class that inherits another Python class with Python's As a MWE of my problem, here is an attempt to translate the following pure Python code (taken from https://realpython.com/python-super/) into its equivalent PythonCall representation. Python: class Rectangle:
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
def perimeter(self):
return 2 * self.length + 2 * self.width
class Square(Rectangle):
def __init__(self, length):
super().__init__(length, length) Julia attempt: using PythonCall
Rectangle = pytype("Rectangle", (), [
"__module__" => "__main__",
pyfunc(
name = "__init__",
function (self, length, width)
self.length = length
self.width = width
return
end
),
pyfunc(
name = "area",
self -> self.length * self.width
),
pyfunc(
name = "perimeter",
self -> 2 * self.length + 2 * self.width
),
])
Square = pytype("Square", (Rectangle,), [
"__module__" => "__main__",
pyfunc(
name = "__init__",
function (self, length)
super().__init__(length, length)
return
end
)
]) Note that |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Not currently possible I'm afraid. You could do |
Beta Was this translation helpful? Give feedback.
Not currently possible I'm afraid. You could do
Rectangle.__init__(self, ...)
instead.