如何在一个类中使用多个__init__构造函数? [英] how to use more than 1 __init__ constructor in a class ?
问题描述
亲们,
有人可以告诉我,如何使用类* *(或模拟更多
而不是1个构造函数) :
# -
class myPointClass:
def __init __(self,x = 0,y = 0):
self.x = x
self.y = y
def __init __(self,x = 0,y = 0,z = 0):
self .__ init __(self,x,y)
self.z = z
# -
tia people
scott
*这不是作业
hi people,
can someone tell me, how to use a class like that* (or "simulate" more
than 1 constructor) :
#--
class myPointClass:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __init__(self, x=0, y=0, z=0):
self.__init__(self, x, y)
self.z = z
#--
tia people
scott
*this is not homework
推荐答案
scott写道:
scott wrote:
有人可以告诉我,如何使用像*那样的类(或模拟比1构造函数更多的
):
# - -
class myPointClass:
def __init __(self,x = 0,y = 0):
self.x = x
self.y = y
def __init__ (self,x = 0,y = 0,z = 0):
self .__ init __(self,x,y)
self.z = z
# -
can someone tell me, how to use a class like that* (or "simulate" more
than 1 constructor) :
#--
class myPointClass:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __init__(self, x=0, y=0, z=0):
self.__init__(self, x, y)
self.z = z
#--
嗯,对于你使用的例子,多个co不需要nstructors。
这将得到与我想象的第一个例子相同的结果:
class myPointClass:
def __init __(self,x = 0,y = 0,z = 0):
self.x = x
self.y = y
self.z = z
-
Michael Hoffman
Well for the example you used, multiple constructors are not needed.
This will get you the same result as what I imagine you wanted the
first example to do:
class myPointClass:
def __init__(self, x=0, y=0, z=0):
self.x = x
self.y = y
self.z = z
--
Michael Hoffman
嗨斯科特,
有人可以告诉我,如何使用像*那样的类(或模拟)更多
比1个构造函数):
一种方法可能是:
class myPointClass:
def __init __(self,** args):
表示k,v表示args.items():
setattr(self,k,v)
*这不是作业
为了安全起见,我会省略解释:)
问候
史蒂夫
2005年6月22日,scott< sc *** @ alussinan.org>写道:嗨人,
有人可以告诉我,如何使用类* *(或模拟更多
而不是1个构造函数):
# - <类myPointClass:
def __init __(self,x = 0,y = 0):
self.x = x
self.y = y
def __init __(self ,x = 0,y = 0,z = 0):
self .__ init __(self,x,y)
self.z = z
# -
tia人
scott
*这不是作业
-
http://mail.python.org/mailman/listinfo/python-list
can someone tell me, how to use a class like that* (or "simulate" more
than 1 constructor) :
One approach could be:
class myPointClass:
def __init__(self, **args):
for k, v in args.items():
setattr(self, k, v)
*this is not homework Just to be safe, I''ll leave out the explanation :)
Regards
Steve
On 6/22/05, scott <sc***@alussinan.org> wrote: hi people,
can someone tell me, how to use a class like that* (or "simulate" more
than 1 constructor) :
#--
class myPointClass:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __init__(self, x=0, y=0, z=0):
self.__init__(self, x, y)
self.z = z
#--
tia people
scott
*this is not homework
--
http://mail.python.org/mailman/listinfo/python-list
我提交给Python食谱的这个食谱
( http://aspn.activestate.com/ASPN/Coo.../Recipe/223611 )
描述了一种执行此操作的技术。我使用创建
颜色对象的示例来绘制位图,使用R,G和B值,
或表示RGB编码值的单个整数。 br />
您描述的这种风格是Java语言工件。如果你有
可选参数,那么这些真的不代表不同的方法
签名。在我的颜色示例中,您确实有不同的签名,
表示颜色组件的3个整数,或单个编码的
整数。
- 保罗
This recipe that I submitted to the Python Cookbook
(http://aspn.activestate.com/ASPN/Coo.../Recipe/223611)
describes a technique for doing this. I use the example of creating
Color objects for plotting to a bitmap, using either R,G,andB values,
or a single integer representing the RGB encoded value.
This style you describe is a Java language artifact. If you have
optional params, then these really don''t represent different method
signatures. In my color example, you truly have different signatures,
either 3 integers representing color components, or a single encoded
integer.
-- Paul
这篇关于如何在一个类中使用多个__init__构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!