Possible Duplicate:
Making a method private in a python subclass
Private Variables and Methods in Python
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
.
Protected variables are those data members of a class that can be accessed within the class and the classes derived from that class. In Python, there is no existence of “Public” instance variables. However, we use underscore '_' symbol to determine the access control of a data member in a class.
Python has no privacy model, there are no access modifiers like in C++, C# or Java. There are no truly 'protected' or 'private' attributes. Names with a leading double underscore and no trailing double underscore are mangled to protect them from clashes when inherited.
Yes, the protected method of a superclass can be overridden by a subclass. If the superclass method is protected, the subclass overridden method can have protected or public (but not default or private) which means the subclass overridden method can not have a weaker access specifier.
While protected members can be accessed anywhere in the same package and outside package only in its child class and using the child class's reference variable only, not on the reference variable of the parent class.
To create the protected variables/methods in the python class, you need to prefix the single underscore ( _) to the name of class variables or methods. Following is the example of creating the protected variables and methods in the python class.
Python - Public, Protected, Private Members Classical object-oriented languages, such as C++ and Java, control the access to class resources by public, private, and protected keywords. Private members of the class are denied access from the environment outside the class.
Protected Members in Python The protected members access limited to the defined class and the classes (sub-classes) that are derived from the defined class. As discussed, in python we don’t have any built-in attributes to implement the protected variables/methods in the python class.
The accessPrivateFunction () method accesses the private members of the class Geek. Below is a program to illustrate the use of all the above three access modifiers (public, protected, and private) of a class in Python:
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.
You can't. Python intentionally does not support access control. By convention, methods starting with an underscore are private, and you should clearly state in the documentation who is supposed to use the method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With