getFoo1() vs. class.foo1
listbox at cybereal.org
Sun Jun 15 04:59:16 CEST 2003
-----BEGIN PGP SIGNED MESSAGE-----
On Saturday 14 June 2003 08:27 am, John J. Lee wrote:
> "Manuel" <manuelbastioniNOSPAM at tin.it> writes:
> > Thanks!
> > I love python: without set and get boring instructions,
> > the class is more simple and readable. :-)
> But note that sometimes, it *is* more readable to give a set_x (and
> maybe get_x) function, if you want to suggest to your users that
> something interesting is going on there.
The funny thing is, right after I read this I had a need for the property
(well, it was a convenience anyway) for this code:
"""Case module to provide an object oriented implentation of a case
"""Case class is designed to work like the case directive in some other
The idea is to subclass the class for your use. i.e.
if you created the subclass like
and then created an instance like: case = mycase("pie")
and then ran case("Hahahah")
then "Hahahah" would be printed on stdio
if you do case.t_item = "something else"
then nothing will happen.
it's worth noting that care should be taken as to which test item strings
spaces are converted to underscore for the inherited definition but, nothing
o_data is assigned in the call but is optional, i.e. case(blah) will assign
blah to o_data.
self.o_data = None
def gett_item(self): return self.__t_item
def sett_item(self, value): self.__t_item = str(value)
def delt_item(self): del self.__t_item
t_item = property(gett_item, sett_item, delt_item, "Test item property")
def __call__(self, testitem, optdata=None):
self.t_item = testitem
self.o_data = optdata
ret = None
if hasattr(self, "case_" + self.t_item.replace(" ", "_")):
func = getattr(self, "case_" + self.t_item.replace(" ", "_"),
#previous line moved down for message width limit
ret = func()
self.o_data = None
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
-----END PGP SIGNATURE-----
More information about the Python-list