Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的受保护方法_Python_Oop_Access Modifiers - Fatal编程技术网

python中的受保护方法

python中的受保护方法,python,oop,access-modifiers,Python,Oop,Access Modifiers,可能重复: 如何在受保护的python类中定义一个方法,并且只有子类才能看到它 这是我的代码: class BaseType(Model): def __init__(self): Model.__init__(self, self.__defaults()) def __defaults(self): return {'name': {}, 'readonly': {}, '

可能重复:

如何在受保护的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
方法。Python故意不支持访问控制。按照惯例,以下划线开头的方法是私有的,您应该在文档中明确说明应该使用该方法的人。

Python不支持C++/Java/C#那样的访问保护。一切都是公开的。校训是“我们在这里都是成年人。”记录你的课程,并坚持让你的合作者阅读并遵循文档

Python中的文化是,以下划线开头的名称意味着,“除非您确实知道应该使用这些名称,否则不要使用它们。”您可以选择以下划线开头您的“受保护”方法。但请记住,这只是一种约定,它不会改变访问方法的方式

以双下划线(
\uu name
)开头的名称会被损坏,因此可以构建继承层次结构,而不必担心名称冲突。有些人将这些用于“私有”方法,但同样,它不会改变访问方法的方式


最好的策略是习惯一个模型,在这个模型中,一个进程中的所有代码都必须编写才能正常工作。

@jamylak No No No No No No No No No No No No No No No No No No No No No No No No No No No No我只想说如何定义一个受保护的方法,我知道下划线为什么是负号???为什么?这是我的问题,我搜索了很多,但没有找到anything@Pooya:你有你的答案:你不能在Python中这样做。描述更大的问题,我们可以帮助您找到适合Python的解决方案。@Pooya,好的,您不能这样做。编写文档,解释开发人员应该调用什么,不应该调用什么。这就是Python的方式。如果你的开发人员不阅读文档,你会遇到更大的问题。先把它修好。是的,我知道这很难,但编译器无法改进您的团队。是的,我知道“和”,但我想知道如何使我的方法受到保护,就像在Java中受到保护一样。答案非常简单,并且已经提供了:在Python中,您不能。@Nedbatcheld
“我们是成年人!”
我希望您能对恶意破坏者和黑客说:(我希望“破坏者和黑客”注释与受保护的方法无关。如果黑客在你的进程中运行他们的代码,那么再多受保护的方法也救不了你。按照惯例,一个下划线被认为是受保护的,两个下划线被认为是私有的。@l\uu flex\uu l:instance属性的双前导下划线使用非常特定的C语义。这不是一个约定,它是语言定义的一部分。根据PEP 8,一个主要的下划线是“弱内部使用指示器”。我认为没有什么类似的C++“保护”和“私有”访问修改器。在PEP 8中,检查“设计继承”一节。。特别是以“另一类属性是属于“子类API”的属性”(在其他语言中通常称为“受保护的”)开头的段落.对我来说,这一部分描述了受保护的属性。也有许多其他人这么认为。我知道互联网上的文章从来都不是证据。但是,有足够多的人同意这一点,所以从定义上讲,这是一种惯例,不是吗?@l_uflex_ul:够公平了。)