需要有关Python类习语的帮助 [英] Need help with Python class idioms
问题描述
你好!
我正试图了解Python并且我已经意识到
有很多Python中的习语与
其他语言略有不同。我有一个类似于下面我所包含的课程。
唯一的区别是在真正的课堂上,我有更多
必修属性。在构造函数上一次指定太多并且
无论如何它们并不总是存在于构造时间,因此访问者必须保留
惯用语。
任何人都可以建议更多Pythony方式来做以下事情吗?
谢谢,
Tad
#!/ bin / env python
A类:
def __init __(自我):
self。 mandatoryVar1 =无
self.mandatoryVar2 =无
def setMV1(self,mv1):
self.mandatoryVar1 = mv1
def setMV2(self,mv2):
self.mandatoryVar2 = mv2
def saveToDB(self,db ):
如果self.mandatoryVar1 ==无:
提高异常,强制性Var 1未设置。
if self。 mandatoryVar2 ==无:
引发异常,强制性Var 2未设置。
#使用db,做任何需要保存的东西。
Howdy!
I''m trying to get my head around Python and I''ve come to realize that
there are a lot of idioms in Python that are a bit different than in
other languages. I have a class similar to what I''ve included below.
The only difference is that in the real class, I have even more
mandatory attributes. Too many to specify at once on a constructor and
they don''t always exist at construction time anyway, so the accessor
idiom must remain.
Can anyone suggest a more Pythony way to do the following?
Thanks,
Tad
#!/bin/env python
class A:
def __init__(self):
self.mandatoryVar1 = None
self.mandatoryVar2 = None
def setMV1(self, mv1):
self.mandatoryVar1 = mv1
def setMV2(self, mv2):
self.mandatoryVar2 = mv2
def saveToDB(self, db):
if self.mandatoryVar1 == None:
raise Exception, "Mandatory Var 1 not set."
if self.mandatoryVar2 == None:
raise Exception, "Mandatory Var 2 not set."
# Using db, do whatever saving stuff is needed.
推荐答案
ta*@tadland.net (Tad Marko)写道:
ta*@tadland.net (Tad Marko) writes:
任何人都可以提出更多Pythony方式来做以下事情吗?
谢谢,
好吧
#!/ bin / env python
A类:
def __init __(自我):
自我.mandatoryVar1 =无
self.mandatoryVar2 =无
def setMV1(self,mv1):
self.mandatoryVar1 = mv1
def setMV2(self ,mv2):
self.mandatoryVar2 = mv2
如果self.mandatoryVar1 ==无:
引发异常,强制性Var 1未设置。
如果self.mandatoryVar2 ==无:
引发异常,强制性Var 2未设置。
#使用db,do需要保存的东西。
Can anyone suggest a more Pythony way to do the following?
Thanks,
Tad
#!/bin/env python
class A:
def __init__(self):
self.mandatoryVar1 = None
self.mandatoryVar2 = None
def setMV1(self, mv1):
self.mandatoryVar1 = mv1
def setMV2(self, mv2):
self.mandatoryVar2 = mv2
def saveToDB(self, db):
if self.mandatoryVar1 == None:
raise Exception, "Mandatory Var 1 not set."
if self.mandatoryVar2 == None:
raise Exception, "Mandatory Var 2 not set."
# Using db, do whatever saving stuff is needed.
A类(对象):
def saveToDB(self,db):
#使用db,做任何需要保存的东西。
' as
class A(object):
def saveToDB(self, db):
# Using db, do whatever saving stuff is needed.
''as
2004年1月21日星期三下午8:43,Tad Marko写道:
On Wednesday 21 January 2004 8:43 pm, Tad Marko wrote:
你好!
我'我试图了解Python,我已经意识到,Python中有很多习惯用法与其他语言有点不同。我有一个类似于下面我所包含的课程。
唯一的区别是,在真正的课堂上,我有更多的强制属性。在构造函数上一次指定太多而且它们在构造时并不总是存在,因此访问者必须保留成语。
任何人都可以建议更多Pythony方式做到以下几点?
谢谢,
#!/ bin / env python
A类:
def __init __(self):
self.mandatoryVar1 =无
self.mandatoryVar2 =无
def setMV1(self,mv1):
self.mandatoryVar1 = mv1
self.mandatoryVar2 = mv2
如果是self.mandatoryVar1 ==无:
引发异常,强制变量1未设置。
如果self.mandatoryVar2 ==无:
引发异常,强制变量2未设置。
#使用db,做任何需要保存的东西。
Howdy!
I''m trying to get my head around Python and I''ve come to realize that
there are a lot of idioms in Python that are a bit different than in
other languages. I have a class similar to what I''ve included below.
The only difference is that in the real class, I have even more
mandatory attributes. Too many to specify at once on a constructor and
they don''t always exist at construction time anyway, so the accessor
idiom must remain.
Can anyone suggest a more Pythony way to do the following?
Thanks,
Tad
#!/bin/env python
class A:
def __init__(self):
self.mandatoryVar1 = None
self.mandatoryVar2 = None
def setMV1(self, mv1):
self.mandatoryVar1 = mv1
def setMV2(self, mv2):
self.mandatoryVar2 = mv2
def saveToDB(self, db):
if self.mandatoryVar1 == None:
raise Exception, "Mandatory Var 1 not set."
if self.mandatoryVar2 == None:
raise Exception, "Mandatory Var 2 not set."
# Using db, do whatever saving stuff is needed.
怎么样:
级A:
def __init __(self,mandatoryVar1,mandatoryVar2):
self.mandatoryVar1 = mandatoryVar1
self.mandatoryVar2 = mandatoryVar2
-
James Henderson,Logical Progression Ltd.
http:// www.logicalprogression.net/
http://sourceforge.net / projects / mailmanager /
2004年1月21日12:43:28 -0800
ta*@tadland.net (Tad Marko)写道:
On 21 Jan 2004 12:43:28 -0800
ta*@tadland.net (Tad Marko) wrote:
你好!
我正试图了解Python并且我已经意识到,Python中有很多习惯用法与其他语言有点不同。我有一个类似于下面我所包含的课程。
唯一的区别是,在真正的课堂上,我有更多的强制属性。在构造函数上一次指定太多而且它们在构造时并不总是存在,因此访问者必须保留成语。
任何人都可以建议更多Pythony方式做到以下几点?
谢谢,
#!/ bin / env python
A类:
def __init __(self):
self.mandatoryVar1 =无
self.mandatoryVar2 =无
def setMV1(self,mv1):
self.mandatoryVar1 = mv1
self.mandatoryVar2 = mv2
如果是self.mandatoryVar1 ==无:
引发异常,强制变量1未设置。
如果self.mandatoryVar2 ==无:
引发异常,强制变量2未设置。
#使用db,做任何需要保存的东西。
Howdy!
I''m trying to get my head around Python and I''ve come to realize that
there are a lot of idioms in Python that are a bit different than in
other languages. I have a class similar to what I''ve included below.
The only difference is that in the real class, I have even more
mandatory attributes. Too many to specify at once on a constructor and
they don''t always exist at construction time anyway, so the accessor
idiom must remain.
Can anyone suggest a more Pythony way to do the following?
Thanks,
Tad
#!/bin/env python
class A:
def __init__(self):
self.mandatoryVar1 = None
self.mandatoryVar2 = None
def setMV1(self, mv1):
self.mandatoryVar1 = mv1
def setMV2(self, mv2):
self.mandatoryVar2 = mv2
def saveToDB(self, db):
if self.mandatoryVar1 == None:
raise Exception, "Mandatory Var 1 not set."
if self.mandatoryVar2 == None:
raise Exception, "Mandatory Var 2 not set."
# Using db, do whatever saving stuff is needed.
mandatoryvariables = dict(mandatoryvar1 = mv1,mandatoryvar2 = mv2 ...)
inst = A()
inst .__ dict __。更新(强制变量)
这应该适合你。
- Josiah
mandatoryvariables = dict(mandatoryvar1=mv1, mandatoryvar2=mv2...)
inst = A()
inst.__dict__.update(mandatoryvariables)
That should work for you.
- Josiah
这篇关于需要有关Python类习语的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!