I have an abstract base class (bottom size
) and I can call it ( circle)
and classes
below).
I call "dispatch class" (called MagicShape
below) so that when I instantiate a new object of this new class, this magically passed parameter On the basis of one of the above sections.
If both circle
and class
are started with 2 parameters, then I want to take 3 parameters for 3 codes so that Parameter can be either a Circle
or square
with either the string circle
or string square
and after the specified parameter Therefore, for specifications, I have:
as the PMI class size Numpy import from pi: def __init __ (self, color, size): self.color = color self.size = description of size def (self): return 'i one (color: s) {Size: s} size { Size: 0.1 F} '. Format (color = self-color, type = self, size = self.size) class circle (define): def __init __ (self, color, size): self Uniform = 'circle' super (circle, self) .__ init __ (color, size) def area (self): return pi * self-compositing * self class class (shape): def __init __ (self, color, size): self .kind = 'class' super (square, self) .__ init __ (color, size) df area (self): return self size * self.size
and I have to do something like this :
So when I ms = MagicShape ('circle', 'red', 3)
, ms
one circle < / Code> But when I type
ms = MagicShape ('square', 'blue', 2)
, ms
a class
.
I know that I can do something like this:
def make_shape (type, color, size): if 'circle' == type: return circle (color) , Size) Alif 'Square' == Type: Return Class (Color, Size) Other: Increasing ValueError
and "Send" by Function. But in some way it was felt that this should happen with classes. Can anyone set me straight?
You can use just one function, no class is required:
< Pre> shapes = {size .__ name___ (Less): shape for shape in shape .__ sub-classes __ ()} def magic shape (type, color, size): try: Return form (except type, size, size) [Key]: Increase ValueError (Type )
here returns all sub-squares of Remember that making a class is just another call . and from the caller's perspective; They use only: for both class and function. size
, which makes it a fast and easy way to create a map by / Code> string in the class
pre>
def Foo (arg1, arg2): Return some_th_i_a_in_in_inins
result = foo (value1, value 2)