python支持保护类型_python中的受保护方法

1586010002-jmsa.png

How can I define a method in a python class that is protected and only subclasses can see it?

This is my code:

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

I want that just subclasses of BaseType see the generateCsCode method of BaseType.

解决方案

Python does not support access protection as C++/Java/C# does. Everything is public. The motto is, "We're all adults here." Document your classes, and insist that your collaborators read and follow the documentation.

The culture in Python is that names starting with underscores mean, "don't use these unless you really know you should." You might choose to begin your "protected" methods with underscores. But keep in mind, this is just a convention, it doesn't change how the method can be accessed.

Names beginning with double underscores (__name) are mangled, so that inheritance hierarchies can be built without fear of name collisions. Some people use these for "private" methods, but again, it doesn't change how the method can be accessed.

The best strategy is to get used to a model where all the code in a single process has to be written to get along.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值