5
I come from PHP. In it, when we want to define a property of a class as private we add the keyword private
in his statement.
Example:
class StackOverflow extends StackExchange
{
private $language = 'en'; // privado, só pode ser acessado via Accessor!
public function __construct($language === null){
if ($language !== null) {
$this->language = $language;
}
}
public function getLanguage()
{
return $this->language;
}
}
Now I’d like to know how to do it in Python
. How to define private property in this example below.
class StackExchange(object):
def __init__(self):
pass
class StackOverflow(StackExchange):
language = 'en' // quero que seja privado ou protegido!
def __init__(self, language = None):
if language is not None:
self.language = language
sopt = StackOverflow('pt')
sopt.language // retorna o valor, mas quero utilizar um Accessor ao invés disso
Another thing: In PHP, we use protected
to define that the property cannot be accessed at the class instantiation, but can be accessed by itself and who inherits it.
You can do this in Python too?
Still, you can access the variable through
_StackOverflow__language
, as in the example he asked for.– Leonel Sanches da Silva
And always will. As said above, there is no concept of private variable.
– user21494
Not a basic little cloister?
– Wallace Maxters
The closest you get to this is using __ in methods or variables. In this case they cannot be called publicly...
– user21494