How often have you used the function range() in Python? It’s often something you use early on as you learn about the for loop…

But… did you know it’s not really a function, after all?!

Have a look at the docs or use help() to check…

range is a class. And therefore range() creates an instance of the class as is always the case with a class.

So, my question is: “Does it matter?”

Technically, it does. A function is not a class – they’re different things

But in practice, what matters is how it behaves and not what it is!

This is a key principle in Python when thinking about data types. What they do and how they behave matters more that what they are!

So you can use range() like a function, even though it’s not!

@s_gruppetta surely, range() *is* a function - a constructor - which _returns_ an instance of a class? I guess the clever bit is that the class acts a bit like an iterator... (though why it's /not/ an iterator is a puzzle to me)

@raymondlesley one could argue the functions are the __new__() and __init__() methods, as with any class, and not the class itself. But the class is callable, like a function, of course

@s_gruppetta @raymondlesley What's the difference between calling a class and calling its initializer?

Also, if range is a class, shouldn't it start with an upper case letter?

@peterdrake @raymondlesley conceptually, I believe there is a difference. One creates a new object based on a template, the other normally performs actions with existing objects.

And the capital letter is just a convention, after all. I believe this is deliberate as the intention is that it’s used “like a function”, even though it isn’t. This comes from Python’s duck-typing philosophy.

@peterdrake @raymondlesley I never said the "behaviour" is different. On the contrary, if you look at my first post, I mention that their behaviour is the same and this is "what matters" in a duck typing language

@s_gruppetta Interesting. I'm going to have to go back to my Python book and see what it says about this. I'm fairly sure it just presents it as a command to be used in a certain way.

@andrewhoyer it “behaves” like a function. In many ways, that’s all that matters!

@s_gruppetta Thanks for sharing! I think this is what APIs of any kind are for - serve the purpose and be easy to understand: no matter if that's a built-in library, some additional package or even REST/SOAP. Clearly, a good example that we should focus on building a good interfaces first.

@s_gruppetta Is this really so different to other languages. The instance created by range() is simply enumerable (including iterators). This concept exists in lot of languages.

@CodingKurzgeschichten I wasn’t claiming in any way this is unique to Python. The point is that we use range as function – and that’s the way it was intended – but it’s not. It’s a class

@CodingKurzgeschichten This fits well within the concept of duck-typing which is true for some language but not others.

What matters here is not what it is but how it behaves, and this is a broader mindset in languages such as Python

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.