I am from PHP and quick learn OOP Python base PHP ‘s knowledge
for example I have a class
<?php
class Cat{
private $name;
private $age;
private $color;
public function getName()
{
return $this->name;
}
public function setName($value)
{
$this->name = $value;
}
public function getAge()
{
return $this->age;
}
public function setAge($value)
{
$this->age = $value;
}
public function getColor()
{
return $this->color;
}
public function setColor($value)
{
$this->color = $value;
}
}
?>
What is equivalence to python OOP code?
An exact equivalent would be this:
It will work, but you would never do that in Python. Here is why:
Here is how you would to it in proper Python:
Now, what if suddenly you want to change your mind and ensure the name is always capitalize on when you store it ? You just add a property:
The code change, but from outside, it looks exactly the same:
What are the main benefits?