>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
class
, последван от блокself
- reference към обекта, на който ги извиквате
class Person:
"""Represents a person."""
people = 0
def __init__(self, name):
"""Constructs a new person."""
self.name = name
Person.people += 1
def say_hi(self):
"""Presents one's self."""
print("Hello, I'm {0}!".format(self.name))
>>> mityo = Person("Mityo the Python")
>>> mityo.say_hi()
Hello, I'm Mityo the Python!
>>> guido = Person("Guido")
>>> guido.say_hi()
Hello, I'm Guido!
>>> print(Person.people)
2
self
class Spam:
def __init__(self, arg):
self.stored = arg
>>> spam = Spam(42)
>>> print(spam.stored)
42
>>> spam.stored = 60
>>> print(spam.stored)
60
>>> spam.foo = 10
>>> print(spam.foo)
10
По този начин може да използвате класовете като структури:
class Student: pass
mityo = Student()
mityo.name = "Mityo the Python"
mityo.age = 22
mityo.faculty_number = 42424
self
.обект.метод(аргументи)
, така и с клас.метод(обект, аргументи)
class Person:
def __init__(self, name):
self.name = name
def greet(self, somebody):
print("Hello {0}, I'm {1}!".format(somebody, self.name))
>>> mityo = Person("Mityo the Python")
>>> mityo.greet('Stefan')
Hello Stefan, I'm Mityo the Python!
>>> Person.greet(mityo, 'Stefan')
Hello Stefan, I'm Mityo the Python!
Интересен страничен (или не толкова страничен) ефект е следното:
>>> person = Person("Mityo the Python")
>>> greet_someone = person.greet
>>> greet_someone("Stefan")
Hello Stefan, I'm Mityo the Python!
Обаче:
>>> greeter = Person.greet
>>> greeter(mityo, "Stefan")
Hello Stefan, I'm Mityo the Python!
При статичните методи положението е малко странно:
class Person:
people = []
def register(name):
Person.people.append(name)
print(len(Person.people), "people are registered now")
register = staticmethod(register)
>>> Person.register("Mityo the Python")
1 people are registered now
>>> Person.register("Pooh")
2 people are registered now
В Python има "класови" методи, които вземат класът на който са извикани като първи аргумент. Понякога е полезно при наследяване:
class Something:
def greet(cls, someone):
print(someone, "was greeted from", cls)
greet = classmethod(greet)
>>> Something.greet("Mityo")
Mityo was greeted from <class '__main__.Something'>
()
върху класа__init__
и взема новосъздадения обект като първи аргумент__new__
, но за него по-нататък
class Person:
def __init__(self, name, age = 20, location = "Sofia"):
self.name = name
self.age = age
self.location = location
>>> person = Person("Mityo the Python", 30, "Great Tarnovo")
Проста хватка:
class Something:
def __init__(self, name): self.name = name
def introduce(self): "This is something called", self.name
class Vegetable(Something):
def eat(self): print(self, "was eaten")
class Animal(Something):
def notice(self): print("Look! This is a", self.name)
def introduce(self): print("This is an animal called", self.name)
>>> snake = Animal("python")
>>> snake.introduce()
This is an animal called python
>>> snake.notice()
Look! This is a python
Стандартното клас.метод
би трябвало да ви е достатъчно:
class PopularPerson(Person):
def greet(self, somebody):
Person.greet(self, somebody)
print("Do you want my autograph?")
>>> pop = PopularPerson("Mityo the Python")
>>> pop.greet("Dim")
Hello Dim, I'm Mityo the Python!
Do you want my autograph?