TI/Skrypty z zajęć/k5

Z Brain-wiki
# -*- coding: utf-8 -*-
"""
Created on Wed May 20 11:17:56 2020

@author: Tomek
"""

class A():
    def __str__(self):
        return 'Jestem obiektem klasy A'
    
    def __call__(self, *args):
        return 'Jestem metoda call wywolana z argumentami '+str(args)
    
    def __getitem__(self,index):
        return str(index)
    
    def __setitem__(self,ind,val):
        print('index =', ind, 'value =', val)
    
a = A()

#a+b = a.__add__(b)
#a(x) = a.__call__(x)
#a[n] = a.__getitem__(x)

class naszSlownik():
    def __init__(self,dic={}):
        self.__sl = dic.copy()
        
    def __str__(self):
        return 'Nasz Slownik ' + str(self.__sl)
    
    def __getitem__(self,index):
        return self.__sl[index]
    
    def __setitem__(self,ind,val):
        if ind == 1:
            print('nie wolno Ci!!!')
            return None
        self.__sl[ind] = val


a = {1:2,2:3,3:4}
b = naszSlownik(a)
a[1] = 100
print(b)