Which of these is a private data field?def Demo:def __init__(self): __a = 1 self.__b = 1 self.__c__ = 1 __d__= 1 __a __b__c____d__
Question
Which of these is a private data field?
def Demo:
def __init__(self):
__a = 1
self.__b = 1
self.__c__ = 1
__d__ = 1
__a __b __c__ __d__
Solution
The private data fields in the given Python class are __a and self.__b.
In Python, private variables begin with double underscores __. However, if they also end with double underscores, they are special variables rather than private variables. Python uses these special variables for specific purposes. For example, __init__ is a special method used for initializing objects.
So, __a and self.__b are private data fields, while self.__c__ and __d__ are not.
Note: Even though __a and self.__b are both private, there is a difference between them. __a is a local variable to the method __init__, while self.__b is a private instance variable of the class.
Similar Questions
class Demo: def __init__(self): self.a = 1 self.__b = 1 def get(self): return self.__bobj = Demo()obj.a=45print(obj.a)
Which of the following is correct?class A: def __init__(self,name): self.name=namea1=A("john")a2=A("john")
what is the output of following python code? class myclass: def __init__(self,a): self.a = a print(self.a)o=myclass()
[True/False] In python classes, the constructor __init__(self) is called implicitly when an object is instantiated of the class.
Which access specifier is usually used for data members of a class?a) Protectedb) Privatec) Publicd) Default
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.