r/learnpython • u/BigBootyBear • Sep 25 '24
Why does Python use list(range(1,100)) when most languages will use something like range(1,100).getList()?
This also applies to using str(someString) vs someString.getStr(). I'm coming from Java and JavaScript and i'm used to utilities existing on the object as opposed to being global functions that you pass values or generators to.
I understand it's a difference in schools of approach on how to get things done using programming. My problem is - why? What are the motivations for having utilities encapsulated within the objects (i.e. obj(foo,bar).doStuff) as opposed to having a global utility that you pass values into like doStuff( obj(foo,bar))?
4
u/backfire10z Sep 25 '24 edited Sep 25 '24
One thing I can think of is static typing. I’m going to ignore JavaScript because every decision there is dumb as hell.
Java is statically* typed. Coming from Java, IntStream returns an object of type IntStream. This has to be cast to a list. Making a list from an IntStream probably looks different from making a list from something else (like a list of keys from a hashmap). They have different implementations and are different types, so you need to accommodate them both when defining the function and using the passed in object. Java decided that it would probably be easier and make more sense for each type to have its own method for creating a list, rather than having a monster method that accepts all types. This allows for better customization and probably readability.
Python, on the other hand, uses duck typing. This means that as long as an object implements the same methods, it can be considered the same type for that purpose. This allows for generic functions like list() to accept a lot of “different” types. In reality, list() calls internal methods “invisible” to you (google dunder methods) that are probably more akin to what you’re used to seeing in Java. Python was also designed to be easy to use for many people, which means making 1 method work almost everywhere is part of their ethos.
Python can use duck typing because of how it is implemented under the hood, where most things are actually objects. This is part of why Python is slow compared to other languages like Java.
Specifically for list(), list() takes any Iterable. An object can be considered an Iterable if it implements a __iter__() and a __next__() method, regardless of what other methods and attributes it may have (this is duck typing in action). Then, list() uses those methods to create a new list by iterating through everything in the passed in object.
2
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Sep 25 '24
"Strongly typed" is not a well-defined term, you're presumably talking about static typing. They're often not the same .
By some definition C is weakly typed, because
void*bypasses the entire type system. Python doesn't have anything like that, you cannot arbitrarily pretend one type is another or not have a type at all.1
1
u/Disastrous-Team-6431 Sep 26 '24
That's not the main reason C is considered weakly typed. It's that C will do type conversion silently between some types.
0
4
u/throwaway6560192 Sep 25 '24
The mistake is to think of list as some utility function.
list is a class, whose constructor takes an iterable as argument. range is an iterable.
2
2
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Sep 25 '24
I wouldn't say most languages do that. Rust kind of does, but there it's basically how traits work and you can extend types with new traits if you want to. The base type doesn't need a method for converting to another type.
2
u/PaulRudin Sep 25 '24
I'm not sure about the "most languages" thing. But it's helpful to understand that a range is capable of being iterated over. If you make custom methods like `.getList()` on a range object then where do you stop? What if you want tuple or whatever then you'll need `.getFoo()` methods for every possible `Foo`.
1
u/Mysterious-Rent7233 Sep 25 '24
You can find these kinds of things in Javascript and Java too (sometimes as Class methods -- which are just global functions in a class namespace).
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
https://www.geeksforgeeks.org/java-string-valueof/
I guess the main thing is that Python avoids imposing on the namespace of the object except for dunder methods. Other methods can be whatever you want and your name will never overlap with a "magic meaning" imposed by Python.
Python's design revolves around functions and protocols implemented as dunder methods:
1
1
u/MidnightPale3220 Sep 25 '24 edited Sep 25 '24
First of all, Python doesn't.
In most cases where a list is expected, range() will be converted implicitly and you just use range() , despite the fact that in Python 3 range is a generator instead of list as it was in Python 2.
Secondly, I believe it's according to official Zen of Python.
https://peps.python.org/pep-0020/ specifically:
Readability counts
Practicality beats purity
This also applies to using str(someString) vs someString.getStr().
You wouldn't do that actually. You would just use someString. If it were a different type of object, you'd use str(someObject) for both the above Zen purposes.
And additionally this way you know you're going to deal with string before even reading whose string representation it is going to be.
But technically you could do someObject.__str__() as well, as that is what will happen behind the scenes.
In my experience, Python more usually uses methods that take an argument, rather than calling empty methods just to return something.
1
u/CranberryDistinct941 Sep 25 '24
The list constructor can take an iterator as an argument, and initialize the list using the elements in that iterator. This can be done with any object that has an __iter__() method
19
u/rasputin1 Sep 25 '24
they actually are encapsulated in the object. you're describing magic methods, research that term. eg str(obj) is actually calling obj.__ str __ under the hood. it is related to a concept called duck typing that python is heavy on. it lets you re use code on objects of varying types without having to modify it based on the type as long as it has that specific function. eg you can call str in your own custom function on a parameter where the parameter can be any type that supports it ie has an __ str __ method.