Quick overview of OOP php to python?

I am from PHP and quickly learn OOP Python base PHP 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 python OOP code equivalence?

+3
source share
5 answers

The exact equivalent would be:

class Cat(object):
    def __init__(self, name...):
        self.__name = name # __ make the var private
        ...

    def getName(self):
        return self.__name

    def setName(self, name):
        self.__name = name

    ...

It will work, but you will never do it in Python. That's why:

  • There is nothing personal in Python, and you can always get around the restrictions using some tricks in this language. That way, you usually value more communications, docs, and APIs that enforce behavior.
  • Direct attribute access is great for Python. If you are not doing something special when getting or setting up, then you are not writing recipients and setters. The best Python libs do this.
  • -, "", . 1 - var, , API.

Python:

class Cat(object):
    def __init__(self, name):
        self.name = name

c = Cat('billy')
print c.name
# billy
c.name = 'kit'
print c.name
# kit

, , , ? :

class Cat(object):

    def __init__(self, name):
        self._name = name # '_' is just a convention and does nothing

    @property # when you do cat.name, it will call this function
    def name(self):
        return self._name

    @name.setter # when you do cat.name = x, it will call this function
    def name(self, name):
        """ Make the name uppercase when setting it """
        self._name = name.upper()

, , :

c = Cat('billy')
print c.name
# BILLY
c.name = 'kit'
print c.name
# KIT

?

  • getter setter, , , , .
  • , ONE getter/setter. , , . API .
+3
class Cat(object):
    def __init__(self, name, age, color):
        self.name = name
        self.age = age
        self.color = color

, .

  • getter/setter, - .
  • , (.. ) AttributeError. , PHP (Python ), " " .
  • Python 2.x object ( ) - . . Python 3 , ( , object, ).
  • : Python ( ), . , Python SO. , .
+8
class Cat(object):
   def __init__(self, name, age, color):
        self.name = name
        self.age = age
        self.color = color

Python; property, - , . , 5 , .

, . C-, Python. , , , . :

def pet(self):
    # Do whatever petting does to a cat.
    pass
+2

, , property.

class Cat(object):
    def __init__(sef, name, age, color):
        self._name = name
        self._age = age
        self._color = color

    def get_name(self):
        return self._name

    def set_name(self, value):
        self._name = value

    def get_age(self):
        return self._age    

    def set_age(self, value):
        self._age = value

    def get_color(self):
        return self._color

    def set_color(self, value):
        self._color = value

    name = property(get_name, set_name)
    age = property(get_age, set_age)
    color = property(get_color, set_color)
+2

this.

However, if you do not need a custom setter and getter, do not do this.

+1
source

All Articles