Exemplo n.º 1
0
        del self._age


# When a Python interpreter reads a source file it executes all its code.
# This __name__ check makes sure this code block is only executed when this
# module is the main program.
if __name__ == '__main__':
    # Instantiate a class
    i = Human(name="Ian")
    i.say("hi")  # "Ian: hi"
    j = Human("Joel")
    j.say("hello")  # "Joel: hello"
    # i and j are instances of type Human, or in other words: they are Human objects

    # Call our class method
    i.say(i.get_species())  # "Ian: H. sapiens"
    # Change the shared attribute
    Human.species = "H. neanderthalensis"
    i.say(i.get_species())  # => "Ian: H. neanderthalensis"
    j.say(j.get_species())  # => "Joel: H. neanderthalensis"

    # Call the static method
    print(Human.grunt())  # => "*grunt*"

    # Cannot call static method with instance of object
    # because i.grunt() will automatically put "self" (the object i) as an argument
    print(i.grunt(
    ))  # => TypeError: grunt() takes 0 positional arguments but 1 was given

    # Update the property for this instance
    i.age = 42
        del self._age


# Когда интерпретатор Python читает исходный файл, он выполняет весь его код.
# Проверка __name__ гарантирует, что этот блок кода выполняется только тогда, когда
# этот модуль - это основная программа.
if __name__ == '__main__':
    # Инициализация экземпляра класса
    i = Human(name="Иван")
    i.say("привет")                 # Выводит: "Иван: привет"
    j = Human("Пётр")
    j.say("привет")                 # Выводит: "Пётр: привет"
    # i и j являются экземплярами типа Human, или другими словами: они являются объектами Human

    # Вызов метода класса
    i.say(i.get_species())          # "Иван: Гомосапиенс"
    # Изменение разделяемого атрибута
    Human.species = "Неандертальец"
    i.say(i.get_species())          # => "Иван: Неандертальец"
    j.say(j.get_species())          # => "Пётр: Неандертальец"

    # Вызов статического метода
    print(Human.grunt())            # => "*grunt*"

    # Невозможно вызвать статический метод с экземпляром объекта
    # потому что i.grunt() автоматически поместит "self" (объект i) в качестве аргумента
    print(i.grunt())                # => TypeError: grunt() takes 0 positional arguments but 1 was given

    # Обновить свойство для этого экземпляра
    i.age = 42
    # Получить свойство
    @age.deleter
    def age(self):
        def self._age

# When a Python interpreter reads a source file it executes all its code.
# This __name__ check makes sure this code block is only executed when this
# module is the main program.
if __name__ == '__main__':
    # Instaniate a class
    i = Human(name = "Ian")
    i.say("hi")
    j = Huam("Joel")
    j.say("hello")
    # call class method
    i.say(i.get_species())
    # set new attributes
    Human.species = "H. neanderhalensis"
    i.say(get_species())
    j.say(j.get_species())

    # call static method
    print(Human.grunt()) #"*grunt*"

# Cannot call static method with instance of object
# because i.grunt() will automatically put "self" (the object i) as an argument
    print(i.grunt()) # TypeError:grunt() takes 0 positional arguments but 1 was given
    # i.grunt() == grunt(i) i is self

    # Update the property(属性) for this instance
    i.age = 42
Exemplo n.º 4
0
        del self._age


# When a Python interpreter reads a source file it executes all its code.
# This __name__ check makes sure this code block is only executed when this
# module is the main program.
if __name__ == '__main__':
    # Instantiate a class
    i = Human(name="Ian")
    i.say("hi")                     # "Ian: hi"
    j = Human("Joel")
    j.say("hello")                  # "Joel: hello"
    # i and j are instances of type Human, or in other words: they are Human objects

    # Call our class method
    i.say(i.get_species())          # "Ian: H. sapiens"
    # Change the shared attribute
    Human.species = "H. neanderthalensis"
    i.say(i.get_species())          # => "Ian: H. neanderthalensis"
    j.say(j.get_species())          # => "Joel: H. neanderthalensis"

    # Call the static method
    print(Human.grunt())            # => "*grunt*"
    
    # Cannot call static method with instance of object 
    # because i.grunt() will automatically put "self" (the object i) as an argument
    print(i.grunt())                # => TypeError: grunt() takes 0 positional arguments but 1 was given
                                    
    # Update the property for this instance
    i.age = 42
    # Get the property