@@ -13,7 +13,8 @@ Create a new type. Equivalent to `type(name, bases, dict)` in Python.
1313
1414If `bases` is not a Python object, it is converted to one using `pytuple`.
1515
16- If `dict` is not a Python object, it is converted to one using `pydict`.
16+ The `dict` may either by a Python object or a Julia iterable. In the latter case, each item
17+ may either be a `name => value` pair or a Python object with a `__name__` attribute.
1718
1819In order to use a Julia `Function` as an instance method, it must be wrapped into a Python
1920function with [`pyfunc`](@ref). Similarly, see also [`pyclassmethod`](@ref),
@@ -24,7 +25,10 @@ to the function always have type `Py`. See the example below.
2425
2526```
2627Foo = pytype("Foo", (), [
27- "__init__" => pyfunc(
28+ "__module__" => "__main__",
29+
30+ pyfunc(
31+ name = "__init__",
2832 doc = \"\"\"
2933 Specify x and y to store in the Foo.
3034
@@ -37,19 +41,22 @@ Foo = pytype("Foo", (), [
3741 end,
3842 ),
3943
40- "__repr__" => function (self)
41- return "Foo(\$ (self.x), \$ (self.y))"
42- end |> pyfunc,
44+ pyfunc(
45+ name = "__repr__",
46+ self -> "Foo(\$ (self.x), \$ (self.y))",
47+ ),
4348
44- "frompair" => pyclassmethod(
49+ pyclassmethod(
50+ name = "frompair",
4551 doc = "Construct a Foo from a tuple of length two.",
4652 (cls, xy) -> cls(xy...),
4753 ),
4854
49- "hello" => pystaticmethod(
55+ pystaticmethod(
56+ name = "hello",
5057 doc = "Prints a friendly greeting.",
5158 (name) -> println("Hello, \$ name"),
52- )
59+ ),
5360
5461 "xy" => pyproperty(
5562 doc = "A tuple of x and y.",
@@ -66,7 +73,7 @@ Foo = pytype("Foo", (), [
6673"""
6774function pytype (name, bases, dict)
6875 bases2 = ispy (bases) ? bases : pytuple (bases)
69- dict2 = ispy (dict) ? dict : pydict (dict)
76+ dict2 = ispy (dict) ? dict : pydict (ispy (item) ? ( pygetattr (item, " __name__ " ) => item) : item for item in dict)
7077 pybuiltins. type (name, bases2, dict2)
7178end
7279
0 commit comments