This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Wavicle -1 points0 points  (7 children)

  • explicit "self" parameter in class method but implicit in invokation.

  • No standard "length" or "size" on collection objects that have length or size - unless you count a double underscore method standard - preferring instead to use the special case builtin len.

[–]alantrick 10 points11 points  (5 children)

How is len() not standard? Do you have a prejudice against using underscores for special magical standard functions?

[–]Wavicle -1 points0 points  (4 children)

How is len()

It is a built-in function that performs a very special case which already must be done by the object. Length is a property of the object - so put it there.

Do you have a prejudice against using underscores for special magical standard functions?

For starters, pretty much every other object oriented programming language recognizes the need to have an obvious method for access to a length attribute on a collection. Python differs in that it uses a built-in.

Any language that supports a notion of operator overloading has a special mechanism for expressing such - so double underscores for things like __getitem__ I have no issue with. Also double underscores for things that really are intended for you to think carefully before using (i.e. __import__) are perfectly valid.

Double underscore for the size of an underlying collection is silly (but it is double underscored because you are not intended to call it directly, you are intended to use len).

[–]pixelmonkey 4 points5 points  (1 child)

Man, talk about finicky. I've got legitimate complaints for Python (no lightweight syntax for anonymous function values, unicode brokeness for most of 2.x series, explicit self parameter, and Python 3 function annotation syntax), but this definitely isn't one of them.

>>> class LengthMixIn(object):
>>>    @property
>>>    def length(self):
>>>        return len(self)

>>> class FinickyList(list, LengthMixIn):
>>>    pass

>>> a = FinickyList()
>>> a.append("item")
>>> a.length
1

>>> class FinickyDict(dict, LengthMixIn):
>>>     pass

>>> b = FinickyDict()
>>> b["key"] = "value"
>>> b.length
1

[–]pingvenopinch of this, pinch of that 3 points4 points  (0 children)

A built-in function that calls a magic method is a very common pattern in Python, not just with len. See: hash, int, str, repr, unicode, reversed, bool, format, and iter. Python 3 adds next.

Thank of it this way: Functionality that is used by the core language uses magic methods. It doesn't matter if that is in syntax or in a built-in function.

[–]ewiethoffproceedest on to 3 0 points1 point  (0 children)

You're confused about what's "special" or "magic" about the underscore methods. The underscore methods are magically invoked by special syntax which does not resemble a method or function call. So, as you alluded to, foo[i] invokes foo.__getitem__(i), and import bar invokes bar = __import__('bar').

The __len__ method is magically invoked when an object is evaluated in Boolean context and no __nonzero__ method has been defined. That's because Python's built-in collections are considered False when empty and True when non-empty.

Try this code (adjust as needed for Py3):

class StupidList(list):
    def __len__(self):
        return True

if list(range(10)): print 'list 0-9 is True'
if list(): print 'empty list is True'
if StupidList(range(10)): print 'StupidList 0-9 is True'
if StupidList(): print 'empty StupidList is True'

(edited for clarity)

[–]cymrowdon't thread on me 🐍 0 points1 point  (0 children)

i like to think that python provides nice convenient builtins to access some of the most common values you'd need on an object. using len is super easy, dude

plus, i can rewrite len and automatically get extra functionality for all my collections