构造函数的成语? [英] idiom for constructor?

查看:100
本文介绍了构造函数的成语?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个很好的Python习惯用于构造函数,可以加速

跟随?


class Foo:

def __init__ (self,a,b,c,d,...):

self.a = a

self.b = b

self.c = c

self.d = d

...


我想保留__init__参数列表显式,就像是,b $ b而不是传入字典,因为我希望代码是明确的

关于它期望的参数...实际上强制执行正确的数字

的论点。

Is there a nice Python idiom for constructors which would expedite the
following?

class Foo:
def __init__(self, a,b,c,d,...):
self.a = a
self.b = b
self.c = c
self.d = d
...

I would like to keep the __init__ parameter list explicit, as is,
rather than passing in a dictionary, as I want the code to be explicit
about what arguments it expects... in effect enforcing the right number
of arguments.

推荐答案

" Mac" <编号****************** @ yahoo.com>写道:
"Mac" <id******************@yahoo.com> writes:
对于构造函数是否有一个很好的Python成语可以加速
以下?

类Foo:
def __init __( self,a,b,c,d,...):
self.a = a
...
Is there a nice Python idiom for constructors which would expedite the
following?

class Foo:
def __init__(self, a,b,c,d,...):
self.a = a
...




你可以试试:


class Foo:

def __init __(self,a,b,c,d):

args = locals( )args.keys()中arg的


如果名字!=''自我'':

self .__ dict __ [arg] = args [arg]


我认为它不会为你节省大量的打字(而且还会给你带来很多丑陋的复杂性)。我发现,当我这样做时,我想要

操纵a,b,c,d。


您可能会看看是否可以自定义您的编辑器使用

模板/交互,然后为您插入正确的文本。

-

Chris Green< cm ** ***@uab.edu>

是的,但是你将宇宙脱离了背景。



You could try:

class Foo:
def __init__(self,a,b,c,d):
args = locals()
for arg in args.keys():
if name!=''self'':
self.__dict__[arg] = args[arg]

I don''t think it saves you a whole lot of typing (and gains you a lot
of ugly complexity). I find as soon as I do this, I then want to
manipulate a,b,c,d .

You might look to see if you can customize your editor to use
templates/interaction and then inserts the right text for you.
--
Chris Green <cm*****@uab.edu>
"Yeah, but you''re taking the universe out of context."


> ;您可能会查看是否可以自定义编辑器以使用
> You might look to see if you can customize your editor to use
模板/交互,然后为您插入正确的文本。
templates/interaction and then inserts the right text for you.




Well ,我并不是真的关心打字量。和

原始形式的内在丑陋,乏味和缺乏优雅一样多。唉,模板/宏不会修复后者。



Well, I''m not really concerned with "amount of typing" as much as with
the inherent ugliness, tediousness, and lack of elegance of the
original form. Alas, templates/macros would not fix the latter.


Mac写道:
Mac wrote:
有没有好的Python构造函数的成语会加速
跟随吗?

类Foo:
def __init __(self,a,b,c,d,...):
self.a = a
self.b = b
self.c = c
self.d = d
...

我想保持__init__参数列表是显式的,而不是传入字典,因为我希望代码是明确的
关于它期望什么参数...实际上强制执行正确的数字
Is there a nice Python idiom for constructors which would expedite the
following?

class Foo:
def __init__(self, a,b,c,d,...):
self.a = a
self.b = b
self.c = c
self.d = d
...

I would like to keep the __init__ parameter list explicit, as is,
rather than passing in a dictionary, as I want the code to be explicit
about what arguments it expects... in effect enforcing the right number
of arguments.



我可以用编程方式列出参数名称:


A类(对象):

def __init __(self,a,b,c,d,e,f,):

varnames = self .__ init __。im_func.func_code.co_varnames

对于varnames中的varname [1:7]:

print varname


a = A(1,2,3,4,5,6)


但我无法得到他们的价值观。


I could list the parameter names programatically:

class A(object):
def __init__(self,a,b,c,d,e,f,):
varnames = self.__init__.im_func.func_code.co_varnames
for varname in varnames[1:7]:
print varname

a = A(1,2,3,4,5,6)

But I could not get their values.


这篇关于构造函数的成语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆