Here is an example which creates a point as p=Point(x, y)
. Assume that I have some array ppp=(x, y)
where x
and y
are numbers and I want to make it of class Point
but in the way: p=Point(ppp)
. I can do either one or another way but not both simultaneously. Is it possible to have both ways?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
If you know that you have a tuple/list while creating the instance, you can do:
p = Point(*ppp)
, whereppp
is the tuple.I would guess that your looking for a way to overload your constructor, as is common in statically typed languages such as C++ and Java.
This is not possible in Python. What you can do is provide different keyword argument combinations, something like:
Which you would then use as:
There are two different ways to acquire the result, the first is to analyse arguments that you pass to __init__ and in dependence of their quantity and type - choose a decision what are you using to instantiate class.
The other decision is to use classmethods as instantiators:
Yes: