Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. class Human(object):
  2.     # A class attribute. It is shared by all instances of this class
  3.     species = "H. sapiens"
  4.  
  5.     # Basic initializer, this is called when this class is instantiated.
  6.     # Note that the double leading and trailing underscores denote objects
  7.     # or attributes that are used by python but that live in user-controlled
  8.     # namespaces. You should not invent such names on your own.
  9.     def __init__(self, name):
  10.         # Assign the argument to the instance's name attribute
  11.         self.name = name
  12.  
  13.         # Initialize property
  14.         self.age = 0
  15.  
  16.     # An instance method. All methods take "self" as the first argument
  17.     def say(self, msg):
  18.         return "{0}: {1}".format(self.name, msg)
  19.  
  20.     # A class method is shared among all instances
  21.     # They are called with the calling class as the first argument
  22.     @classmethod
  23.     def get_species(cls):
  24.         return cls.species
  25.  
  26.     # A static method is called without a class or instance reference
  27.     @staticmethod
  28.     def grunt():
  29.         return "*grunt*"
  30.  
  31.     # A property is just like a getter.
  32.     # It turns the method age() into an read-only attribute
  33.     # of the same name.
  34.     @property
  35.     def age(self):
  36.         return self._age
  37.  
  38.     # This allows the property to be set
  39.     @age.setter
  40.     def age(self, age):
  41.         self._age = age
  42.  
  43.     # This allows the property to be deleted
  44.     @age.deleter
  45.     def age(self):
  46.         del self._age
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement