accessors and lazy initialization

B

Bill

Hello --

I'm a Java programmer who's slowly getting up to speed in Python. In
general I try to initialize the state of my objects as late as
possible, in the accessor. So if I have a member "_foo", my accessor
is something like:

public FooType getFoo() {
if (_foo == null) {
// initialize _foo here
}
return _foo;
}

I'd like to do lazy initialization in Python as well:

def getFoo():
try:
return _foo
except AttributeError:
# initialize _foo here
return _foo

This seems a little clunky. Is there a more Pythonic way of looking at
this?

Thanks -- Bill.
 
N

Neil Dunn

The second version seems the most sensible. Remeber this is python,
properties are more sensible then getters and setters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top