Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52638

Re: Python getters and setters

Date 2013-08-17 12:31 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: Python getters and setters
References <227bd47c-0e86-4d65-985f-e59aa4f25294@googlegroups.com> <520fb04f$0$30000$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.18.1376760589.23369.python-list@python.org> (permalink)

Show all headers | View raw


On 2013-08-17 17:18, Steven D'Aprano wrote:
> # Yes, this is good, consistent design
> len(myrecord.field)
> len(obj.data)
> len(data.value)
> len(collection[key])

I would also add that, if the primary goal of your class is to
encapsulate the data, you can do

  class MyClass:
    def __init__(self, ...):
      self.data = []
    def __len__(self):
      return len(self.data)

which allows for the even clearer

  my_obj = MyClass(...)
  manipulate(my_obj)
  if len(my_obj) > 42:
    do_important_stuff()

-tkc

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Python getters and setters Fernando Saldanha <fsaldan1@gmail.com> - 2013-08-17 09:53 -0700
  Re: Python getters and setters MRAB <python@mrabarnett.plus.com> - 2013-08-17 18:10 +0100
  Re: Python getters and setters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-17 17:18 +0000
    Re: Python getters and setters Tim Chase <python.list@tim.thechases.com> - 2013-08-17 12:31 -0500
    Re: Python getters and setters Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2013-08-18 00:52 +0200
      Re: Python getters and setters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-17 23:40 +0000
        Re: Python getters and setters Chris Angelico <rosuav@gmail.com> - 2013-08-18 02:07 +0100
  Re: Python getters and setters Fernando Saldanha <fsaldan1@gmail.com> - 2013-08-17 22:26 -0700

csiph-web