Using python's super() within a new Julia-defined Python class #423
Answered
by
cjdoris
bensetterholm
asked this question in
Q&A
|
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 |
Answered by
cjdoris
Nov 22, 2023
Replies: 1 comment 2 replies
|
Not currently possible I'm afraid. You could do |
2 replies
Answer selected by
bensetterholm
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not currently possible I'm afraid. You could do
Rectangle.__init__(self, ...)instead.