class T2:
def total(self,s):
self.total = len(str(s))
t1 = T1()
t2 = T2()
t1.total(45)
t2.total(45)
print(t1.total) # Вывод: 55
print(t2.total) # Вывод: 2
# class Rectangle:
def __init__(self, width, height, sign):
self.w = int(width)
self.h = int(height)
self.s = str(sign)
def __str__(self):
rect = []
for i in range(self.h): # количество строк
rect.append(self.s * self.w) # знак повторяется w раз
rect = '\n'.join(rect) # превращаем список в строку
return rect
b = Rectangle(10, 3, '*')
print(b)
# class A:
def __init__(self, v1, v2):
self.field1 = v1
self.field2 = v2
def __str__(self):
return str(self.field1) + " " + str(self.field2)
a = A(3, 4)
print(a)
# inkapsulyatsiya # class A:
def _private(self):
print("Это приватный метод!")
>>> a = A()
>>> a._private()
Это приватный метод!
# class C(ABC):
@abstractmethod
def my_abstract_method(self, ...):
...
@classmethod
@abstractmethod
def my_abstract_classmethod(cls, ...):
...
@staticmethod
@abstractmethod
def my_abstract_staticmethod(...):
...
@property
@abstractmethod
def my_abstract_property(self):
...
@my_abstract_property.setter
@abstractmethod
def my_abstract_property(self, val):
...
@abstractmethod
def _get_x(self):
...
@abstractmethod
def _set_x(self, val):
...
x = property(_get_x, _set_x)
# class Descriptor:
...
@property
def __isabstractmethod__(self):
return any(getattr(f, '__isabstractmethod__', False) for
f in (self._fget, self._fset, self._fdel))
# class Foo:
def __getitem__(self, index):
...
def __len__(self):
...
def get_iterator(self):
return iter(self)
class MyIterable(ABC):
@abstractmethod
def __iter__(self):
while False:
yield None
def get_iterator(self):
return self.__iter__()
@classmethod
def __subclasshook__(cls, C):
if cls is MyIterable:
if any("__iter__" in B.__dict__ for B in C.__mro__):
return True
return NotImplemented
MyIterable.register(Foo)
# public # class employee:
def __init__(self, name, sal):
self.name=name
self.salary=sal
# >>> e1=Employee("Kiran",10000)
>>> e1.salary
10000
>>> e1.salary=20000
>>> e1.salary
20000
# Example: Protected Attributes
class employee:
def __init__(self, name, sal):
self._name=name # protected attribute
self._salary=sal # protected attribute
# >>> e1=employee("Swati", 10000)
>>> e1._salary
10000
>>> e1._salary=20000
>>> e1._salary
20000
# Example: Private Attributes
class employee:
def __init__(self, name, sal):
self.__name=name # private attribute
self.__salary=sal # private attribute
>>> e1=employee("Bill",10000)
>>> e1.__salary
AttributeError: 'employee' object has no attribute '__salary'
>>> e1=Employee("Bill",10000)
>>> e1._Employee__salary
10000
>>> e1._Employee__salary=20000
>>> e1._Employee__salary
20000
# Example: person.py
class person:
def __init__(self, name="Guest"):
self.__name=name
def setname(self, name):
self.__name=name
def getname(self):
return self.__name
# >>> from person import person
>>> p1=person()
>>> p1.getname()
'Guest'
>>> p1.setname('Bill')
>>> p1.getname()
'Bill'
# Example: property() function
class person:
def __init__(self):
self.__name=''
def setname(self, name):
print('setname() called')
self.__name=name
def getname(self):
print('getname() called')
return self.__name
name=property(getname, setname)
# >>> from person import person
>>> p1=person()
>>> p1.name="Steve"
setname() called
>>> p1.name
getname() called
'Steve'
# Example: property() function
class person:
def __init__(self, name):
self.__name=name
def setname(self, name):
print('setname() called')
self.__name=name
def getname(self):
print('getname() called')
return self.__name
def delname(self):
print('delname() called')
del self.__name
name=property(getname, setname, delname)
# >>> from person import person
>>> p1=person()
>>> p1.name="Steve"
setname() called
>>> del p1.name
delname() called
#
Do'stlaringiz bilan baham: |