4 ms·
> it’s sometimes asked why len in Python is a function, rather than a method. The answer is that of course it is a method, called __len__. The real answer is th
by zak_mc_kracken 12y ago
> it’s sometimes asked why len in Python is a function, rather than a method. The answer is that of course it is a method, called __len__. The real answer is that Python pointedly and deliberately does not reserve any method
That's some comical post rationalization.
The reason why len is a function is because it was in Python before Python started receiving OO features.
As a result, Python is a crazy hodge podge of imperative, OO and functional semantics where you never know if you should be calling `o.foo()` or `foo(o)` unless you've been writing Python since the late 90s.
- pekk 12y agoThis is a myth. Python supported classes close to the start of its existence, way before it became popular.
- sanderjd 12y agoI really like the approach of allowing either kind of call, with the "dot" style mere convenience over the function style. Rust has that sort of thing: let v = &vec![1u32,2,3]; println!("{}", v.len()); println!("{}", Vec::len(v));
- kibwen 12y agoIn general this feature is sometimes called "Universal Function Call Syntax" (UFCS), though interestingly Rust does it exactly the opposite of most other languages with this feature. In Rust, defining a method lets you use it as a free function; in C# and D, defining a free function lets you use it as a method.
- deleted 12y ago[deleted]
- skatenerd 12y agoI always thought this was a weird nod to typeclasses
- andybak 12y agoIf you have some evidence to back that up then you'll want to correct http://en.wikipedia.org/wiki/History_of_Python http://en.wikipedia.org/wiki/History_of_Python "In February 1991, Van Rossum published the code (labeled version 0.9.0) to alt.sources. Already present at this stage in development were classes with inheritance, exception handling, functions [...]"