Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

The two ways you proposed are both good ideas. Let me throw in one more: tuples! Tuples are immutable.

@property
def bananas(self):
    return tuple(self._bananas)

Now that you have these alternative, there are a couple of things of things to keep in mind while choosing one over the other:

  • Is the list small and are you okay with a O(n) accessor? Choose tuple. For most part the consumer is not going to see a difference. (Unless of course he tries to mutate it)
  • Does the list bananas need some special abilities that a generic list is falling short on? Subclass a list and raise exceptions on mutating functions. [1]

[1]: jsbueno has a nice ReadOnlyList implementationnice ReadOnlyList implementation that doesn't have the O(n) overhead.

The two ways you proposed are both good ideas. Let me throw in one more: tuples! Tuples are immutable.

@property
def bananas(self):
    return tuple(self._bananas)

Now that you have these alternative, there are a couple of things of things to keep in mind while choosing one over the other:

  • Is the list small and are you okay with a O(n) accessor? Choose tuple. For most part the consumer is not going to see a difference. (Unless of course he tries to mutate it)
  • Does the list bananas need some special abilities that a generic list is falling short on? Subclass a list and raise exceptions on mutating functions. [1]

[1]: jsbueno has a nice ReadOnlyList implementation that doesn't have the O(n) overhead.

The two ways you proposed are both good ideas. Let me throw in one more: tuples! Tuples are immutable.

@property
def bananas(self):
    return tuple(self._bananas)

Now that you have these alternative, there are a couple of things of things to keep in mind while choosing one over the other:

  • Is the list small and are you okay with a O(n) accessor? Choose tuple. For most part the consumer is not going to see a difference. (Unless of course he tries to mutate it)
  • Does the list bananas need some special abilities that a generic list is falling short on? Subclass a list and raise exceptions on mutating functions. [1]

[1]: jsbueno has a nice ReadOnlyList implementation that doesn't have the O(n) overhead.

added 125 characters in body
Source Link
UltraInstinct
  • 44.6k
  • 12
  • 85
  • 108

The two ways you proposed are both good ideas. Let me throw in one more: tuples! Tuples are immutable.

@property
def bananas(self):
    return tuple(self._bananas)

Now that you have these alternative, there are a couple of things of things to keep in mind while choosing one over the other:

  • Is the list small and are you okay with a O(n) accessor? Choose tuple. For most part the consumer is not going to see a difference. (Unless of course he tries to mutate it)
  • Does the list bananas need some special abilities that a generic list is falling short on? Subclass a list and raise exceptions on mutating functions. [1]

[1]: jsbueno has a nice ReadOnlyList implementation that doesn't have the O(n) overhead.

The two ways you proposed are both good ideas. Let me throw in one more: tuples! Tuples are immutable.

@property
def bananas(self):
    return tuple(self._bananas)

Now that you have these alternative, there are a couple of things of things to keep in mind while choosing one over the other:

  • Is the list small and are you okay with a O(n) accessor? Choose tuple. For most part the consumer is not going to see a difference. (Unless of course he tries to mutate it)
  • Does the list bananas need some special abilities that a generic list is falling short on? Subclass a list and raise exceptions on mutating functions.

The two ways you proposed are both good ideas. Let me throw in one more: tuples! Tuples are immutable.

@property
def bananas(self):
    return tuple(self._bananas)

Now that you have these alternative, there are a couple of things of things to keep in mind while choosing one over the other:

  • Is the list small and are you okay with a O(n) accessor? Choose tuple. For most part the consumer is not going to see a difference. (Unless of course he tries to mutate it)
  • Does the list bananas need some special abilities that a generic list is falling short on? Subclass a list and raise exceptions on mutating functions. [1]

[1]: jsbueno has a nice ReadOnlyList implementation that doesn't have the O(n) overhead.

Source Link
UltraInstinct
  • 44.6k
  • 12
  • 85
  • 108

The two ways you proposed are both good ideas. Let me throw in one more: tuples! Tuples are immutable.

@property
def bananas(self):
    return tuple(self._bananas)

Now that you have these alternative, there are a couple of things of things to keep in mind while choosing one over the other:

  • Is the list small and are you okay with a O(n) accessor? Choose tuple. For most part the consumer is not going to see a difference. (Unless of course he tries to mutate it)
  • Does the list bananas need some special abilities that a generic list is falling short on? Subclass a list and raise exceptions on mutating functions.