Hi,
I'm playing with __slot__ (new class) and the __init__ method of my
class. I want the arguments of __init__ to be keywords and that the
method initialize all the attributes in __slot__ to None except those
in the argument of __init__ (Is this a good practice?). I've the
following class
class PersonalData(ob ject):
__slot__ = ("Firstname" , # Firstname of the person.
"Surname", # Surname of the person.
"Birthdate" , # Birthdate of the person.
"Nationalit y" # Nationality of the person.
)
def __init__(self,* *args):
for kwrd, value in args.items():
self.__setattr_ _(kwrd,args[kwrd]
# for arg,value in args.items():
# if arg in self.__slot__:
# self.__setattr_ _(arg,value)
# else:
# errmsg = str(self.__clas s__.__name__)
# errmsg += "object has not attribute " + arg
# raise AttributeError, errmsg
def __str__(self):
buffer = ""
for attr in self.__slot__:
if attr in self.__dict__.k eys():
buffer += attr + ": " + str(self.__geta ttribute__(attr )) + "\n"
The problem is that the __init__ defined can set attributes that are
not in the slot and i want to have an exception when someone tries to
do
inst = PersonalData(no attribute = "Smith")
I've coded the solution in the fragment that is commented, raising and
exception manually. Someone knows a more elegant solution and can
explain with the first definition didn't raise and exception?
Thanks in advance
Zunbeltz
--
Remove XXX from email: zunbeltz@wm.lc. ehu.esXXX
Comment