What's New in Teams Calling, Meetings and Devices March 2024
Magicke metody v Pythonu
1. __magické__ metody
v Pythonu(2.x)
(ochutnávka)
Jirka Vejražka (@JirkaV)
@naPyVo #22
2. class Person(models.Model):
name = models.CharField(max_length=128)
def __unicode__(self):
return self.name
class MojeTrida(object):
def __init__(self, data):
self.data = data
4. >>> mystring = 'My hovercraft if full of eels'
>>> if mystring.index('full') > 0:
>>> if ‘full’ in mystring:
object.__contains__(self, item)
Called to implement membership test operators. Should return
true if item is in self, false otherwise. For mapping objects, this
should consider the keys of the mapping rather than the values
or the key-item pairs.
5. >>> 'full' in 'My hovercraft is full of eels‘
True
>>> class MyStr(str):
... def __contains__(self, elem):
... return False
>>> mystring = MyStr('My hovercraft is full of eels')
>>> mystring
'My hovercraft is full of eels'
>>> 'full' in mystring
False