Обектно-ориентирано програмиране
class Vector:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
spam = Vector(1.0, 2.0, 3.0)
print(spam.x)
class Vector:
def __init__(self, x, y, z): ...
def length(self):
return (self.x * self.x + self.y * self.y + self.z * self.z) ** 0.5
spam = Vector(1.0, 2.0, 3.0)
print(spam.length())
class Vector:
def __init__(self, x, y, z): ...
def _coords(self):
return (self.x, self.y, self.z)
def length(self):
return sum(_ ** 2 for _ in self._coords()) ** 0.5
v1 = Vector(1.0, 2.0, 3.0)
v2 = Vector(4.0, 5.0, 6.0)
v3 = Vector(7.0, 8.0, 9.0)
print(Vector.length(v1))
print(Vector.length(v2))
print(list(map(Vector.length, [v1, v2, v3])))
class Vector:
def __init__(self, x, y, z): ...
def length(self): ...
def normalize(self):
length = self.length()
self.x /= length
self.y /= length
self.z /= length
class Vector:
def __init__(self, x, y, z): ...
def length(self): ...
def normalize(self):
length = self.length()
return Vector(self.x / length,
self.y / length, self.z / length)
class Vector:
...
def normalize(self):
length = self.length()
self.x /= length
self.y /= length
self.z /= length
def normalized(self):
return Vector(self.x / self.length(), self.y / self.length(),
self.z / self.length())
class Vector:
def __init__(self, x, y, z):
self._coords = (x, y, z)
def __eq__(self, other):
return self._coords == other._coords
Можете да предефинирате аритметичните оператори за вашите типове.
Има методи, които може да предефинирате, за преобразования от вашия клас към стандартен тип
Можете да предефинирате оператора две скоби ().
class Stamp:
def __init__(self, name): self.name = name
def __call__(self, something):
print("{0} was stamped by {1}".format(something, self.name))
>>> stamp = Stamp("The government")
>>> stamp("That thing there")
That thing there was stamped by The government
Можете да "декорирате" методи, така че те да станат статични -- т.е. да нямат нужда от инстанция
class GoatSimulator:
goats = []
@staticmethod
def register(name):
GoatSimulator.goats.append(name)
print(len(GoatSimulator.goats), " goats are registered now")
>>> GoatSimulator.register("Pip the Happy Goat")
1 goats are registered now
>>> GoatSimulator.register("George the Gutsy Goat")
2 goats are registered now
Можете да използвате и @classmethod, за да получите класа от който е извикан метода като първи аргумент
class Something:
@classmethod
def greet(cls, someone):
print(someone, "was greeted from", cls)
>>> Something.greet("Stefan")
Stefan was greeted from <class '__main__.Something'>
Декораторът @property
може се използва за да накарате някой метод да се преструва на property
class Goat:
def __init__(self, name, trait):
self.name = name
self.trait = trait
@property
def description(self):
return "{} the {} goat".format(self.name, self.trait)
>>> g = Goat("George", "Gutsy")
>>> g.description
'George the Gutsy goat'
class Color:
def __init__(self, rgba):
self._rgba = tuple(rgba)
@property
def rgba(self):
return self._rgba
@rgba.setter
def rgba(self, value):
self._rgba = tuple(value)
>>> red = Color([255,0,0])
>>> red.rgba
(255, 0, 0)
>>> red.rgba = [127,0,0]
>>> red.rgba
(127, 0, 0)
def addition(a, b):
return Vector(a.x + b.x, a.y + b.y, a.z + b.z)
class Vector:
def __init__(self, x, y, z): ...
__add__ = addition
print(Vector(1.0, 2.0, 3.0) + Vector(4.0, 5.0, 6.0))