getFoo1() vs. class.foo1

Zac Jensen listbox at cybereal.org
Sat Jun 14 22:59:16 EDT 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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.
>
>
> John

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:

#casemod.py

"""Case module to provide an object oriented implentation of a case 
structure."""

class Case:
  """Case class is designed to work like the case directive in some other 
languages.

  The idea is to subclass the class for your use. i.e.
  if you created the subclass like
  class mycase(Case):
    def case_pie(self):
      print self.o_data

  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 
you use
  spaces are converted to underscore for the inherited definition but, nothing 
else.
  o_data is assigned in the call but is optional, i.e. case(blah) will assign 
blah to o_data.
  """

  def __init__(self):
    UserDict.__init__(self)
    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 default(self):
    return bool(0)

  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(" ", "_"),
                                                         self.default)
        #previous line moved down for message width limit
        if callable(func):
          ret = func()
    self.o_data = None
    return ret
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE+6+EEV3L7YsSif1URApg7AJ9exbYR/iby94TNcERm+cBY6ksfWwCfU6Ew
E8Am8sghbVDGOi2B5g7Wqlo=
=kkNe
-----END PGP SIGNATURE-----






More information about the Python-list mailing list