I come from PHP. In it, when we want to define a property of a class as private we add the keyword private
in its declaration.
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 this in Python
. How to set 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 can not be accessed in class instantiation, but can be accessed by itself and by who inherits it.
Can you do this in Python too?