design question: no new attributes
Ben Finney
bignose+hates-spam at benfinney.id.au
Tue Feb 27 02:47:22 EST 2007
"Alan Isaac" <aisaac at american.edu> writes:
> "Ben Finney" <bignose+hates-spam at benfinney.id.au> wrote:
> > The Pythonic design is: don't expect to have such control over
> > users of your code.
>
> I know this is a popular response, but the fact of the matter
> remains that it can be helpful to know when someone tries to set a
> value for a nonexisting attribute.
That's quite a different request from your original request asking the
Pythonic design for *preventing* setting of new attributes.
> This is especially true if there have been any interface changes.
If the interface has changed, then you don't need to trap *every*
setting of an attribute; only the names that you know have changed.
===== foo_v1.py =====
class Foo(object):
def bar(self):
return 17
spam = 42
=====
===== foo_v2.py =====
import logging
class Foo(object):
def baz(self):
return 13
eggs = 69
def bar(self):
""" Obsolete interface for Foo.baz() """
logging.warn("Deprecated Foo.bar() call, please use Foo.baz()")
return self.baz()
def _get_spam(self):
""" Obsolete interface for Foo.eggs """
logging.warn("Deprecated Foo.spam reference, please use Foo.eggs")
return self.eggs
def _set_spam(self, value):
""" Obsolete interface for Foo.eggs """
logging.warn("Deprecated Foo.spam reference, please use Foo.eggs")
self.eggs = value
spam = property(_get_spam, _set_spam)
=====
After a reasonable grace period, you can drop these compatibility
interfaces in a future version.
--
\ "For every complex problem, there is a solution that is simple, |
`\ neat, and wrong." -- Henry L. Mencken |
_o__) |
Ben Finney
More information about the Python-list
mailing list