tags:

views:

1140

answers:

2
items = []
items.append("shrooms")
items.append("meth")
items.append("weed")

// FAKE METHOD::
items.amount()  // should return 3

How I do it right?

A: 

len(items)

Try google next time

John Millikin
That isn't very OOP, wtf Python is noobly
Joshua
"isn't very OOP"? Semantics decides whether a solution is OO, not syntax. There's no difference between `len(items)` and `items.len()`.
John Millikin
You could say items.__len__() if you really need object.method syntax.
Corey Porter
Actually, it's a standard way of asking any sequence or mapping for its length. Seems pretty object oriented to me.
gnud
\_\_methods\_\_ are not supposed to be called directly.
Juanjo Conti
Re: the assertion that `len(foo)`"isn't very OOP": seems to display a distressingly "cargo cult" mentality. It isn't OOP if it doesn't "look OOPy?" `len()` attempts to call the `__len__()` method on any object that has it (internally using introspection in the process). This is perfectly object oriented.
Jim Dennis
+9  A: 

The len function can be used with a lot of types in python - both built-in types and library types.

>>len([1,2,3])
3
gnud
If making it cosmetically more OOP is important to you .. call the `.__len__()` method on it. `[0,1,3].__len__()` -> 3
Jim Dennis
(Ooops, meant to comment on other response).
Jim Dennis
I gave answer to you cause you werent mean and condescending in your answer!!
Joshua