Skutecna zlodejna neni tak hezky deklarovana, ale dela se pekne pod rukou: a = SuperMaxiSecureAccount() a.__class__ = Thief
On 05/12/2015 12:46 AM, Jan Bednařík wrote: > Trošku z jiného soudku: > > $ python3 > Python 3.4.3 (default, Mar 23 2015, 04:19:36) > [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin > Type "help", "copyright", "credits" or "license" for more information. >>>> >>>> class Account: > ... def deposit(self, amount): > ... print('saved:', amount) > ... >>>> class SecureAccount(Account): > ... def safe_deposit(self, amount): > ... print('*secure transaction begin*') > ... super().deposit(amount) > ... print('*secure transaction end*') > ... >>>> sa = SecureAccount() >>>> sa.safe_deposit(100) > *secure transaction begin* > saved: 100 > *secure transaction end* >>>> >>>> class Thief(Account): > ... def deposit(self, amount): > ... print('STOLEN:', amount) > ... >>>> class MyAccount(SecureAccount, Thief): > ... pass > ... >>>> ma = MyAccount() >>>> ma.safe_deposit(200) > *secure transaction begin* > STOLEN: 200 > *secure transaction end* >>>> > > Honza > > 2015-05-11 23:57 GMT+02:00 Matěj Cepl <mc...@cepl.eu > <mailto:mc...@cepl.eu>>: > > On 2015-05-11, 20:53 GMT, Honza Král wrote: > > Jen pozor, ze tohle je chovani CPythonu, tedy jedne z implementaci a > > nikoli definice chovani pythonu, nelze tedy na to spolehat a > > porovnavani integereru by se melo vzdy provadet pres == a nikdy pres > > is, i kdybychom meli 1000% jistotu, ze to budou jen cisla od 0 do 10. > > Honza Král > > Skutečně, neni Python jako Python: > > $ jython > Jython 2.7b4 (default:3672e624962a, Feb 13 2015, 04:59:14) > [OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.7.0_79 > Type "help", "copyright", "credits" or "license" for more > information. > >>> a = 256 > >>> b = 256 > >>> a is b > True > >>> a = 257 > >>> b = 257 > >>> a is b > True > >>> > $ > > Zajímavě, > > Matěj > -- > http://www.ceplovi.cz/matej/, Jabber: mcepl<at>ceplovi.cz > <http://ceplovi.cz> > GPG Finger: 89EF 4BC6 288A BF43 1BAB 25C3 E09F EF25 D964 84AC > > The ratio of literacy to illiteracy is a constant, but nowadays > the illiterates can read. > -- Alberto Moravia > _______________________________________________ > Python mailing list > python@py.cz <mailto:python@py.cz> > http://www.py.cz/mailman/listinfo/python > > Visit: http://www.py.cz > > > > > _______________________________________________ > Python mailing list > python@py.cz > http://www.py.cz/mailman/listinfo/python > > Visit: http://www.py.cz > _______________________________________________ Python mailing list python@py.cz http://www.py.cz/mailman/listinfo/python Visit: http://www.py.cz