可能重複:
Making a method private in a python subclass
Private Variables and Methods in Python受保護的方法
我如何定義在被保護,只有子類可以看到一個Python類的方法?
這是我的代碼:
class BaseType(Model):
def __init__(self):
Model.__init__(self, self.__defaults())
def __defaults(self):
return {'name': {},
'readonly': {},
'constraints': {'value': UniqueMap()},
'cType': {}
}
cType = property(lambda self: self.getAttribute("cType"), lambda self, data: self.setAttribute('cType', data))
name = property(lambda self: self.getAttribute("name"), lambda self, data: self.setAttribute('name', data))
readonly = property(lambda self: self.getAttribute("readonly"),
lambda self, data: self.setAttribute('readonly', data))
constraints = property(lambda self: self.getAttribute("constraints"))
def getJsCode(self):
pass
def getCsCode(self):
pass
def generateCsCode(self, template=None, constraintMap=None, **kwargs):
if not template:
template = self.csTemplate
if not constraintMap: constraintMap = {}
atts = ""
constraintMap.update(constraintMap)
for element in self.getNoneEmptyAttributes():
if not AbstractType.constraintMap.has_key(element[0].lower()):
continue
attTemplate = Template(AbstractType.constraintMap[element[0].lower()]['cs'])
attValue = str(element[1]['value'])
atts += "%s " % attTemplate.substitute({'value': attValue})
kwargs.update(dict(attributes=atts))
return template.substitute(kwargs)
class MainClass(BaseType, Model):
def __init__(self):
#Only Model will initialize
Model.__init__(self, self.__defaults())
BaseType.__init__(self)
def __defaults(self):
return {'name': {},
'fields': {'value': UniqueMap()},
'innerClass': {'value': UniqueMap()},
'types': {}
}
fields = property(lambda self: self.getAttribute("fields"))
innerClass = property(lambda self: self.getAttribute("innerClass"))
types = property(lambda self: self.getAttribute("types"))
@staticmethod
def isType(iType):
# return type(widget) in WidgetSelector.widgets.itervalues()
return isinstance(iType, AbstractType)
def addType(self, type):
if not MainClass.isType(type):
raise Exception, "Unknown widget type %s" % type
self.types[type.name] = type
我想那只是BaseType
子看到的BaseType
的generateCsCode
方法。
@jamylak否否否否否我只想說如何定義受保護的方法,我知道下劃線 – Pooya 2012-07-14 11:44:53
爲什麼減去???爲什麼?這是我的問題,我搜索了很多,我沒有找到任何東西 – Pooya 2012-07-14 11:46:08
@Pooya:你有你的答案:你不用Python做這個。描述更大的問題,我們可以幫助您找到適合Python的解決方案。 – 2012-07-14 11:47:13