[Solved] Parameter type and Argument type: Why is this argument, that already looks like a list, explicitly passed into list()? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

[Solved] Parameter type and Argument type: Why is this argument, that already looks like a list, explicitly passed into list()?

It's in the Data Hiding section of Python Core SoloLearn, about which I guess, I'll be asking more questions. But is [1, 2, 3] a "none" (nontype?) here? I couldn't get to print type "contents". Are parameters nontypes as a rule? Why don't they get automatically assigned a type when an argument's passed? if they are positional and not variable, why would percieving the [1,2,3] in queue = Queue([1,2,3]) as a list not be a thing? Is, it also to allow strings made of solely numbers into the argument and turn them into a list as an attribute? I just don't want to misunderstand something that might in fact be fundamental. Thanks to all the responders in advance. https://code.sololearn.com/c2aSKNF7LD9k/?ref=app

7th Apr 2022, 12:42 PM
KorkunƧ el Gato
KorkunƧ el Gato - avatar
8 Respostas
+ 2
Nope, I mean you can, but because it's already a string it won't do anything. Just like calling list() on a list will do nothing. A prime example is when people just start using the input() method. That method returns a string by default, but you'll see lots of begginer code looking like: name = str(input("Enter name: ")) and it causes no error but it's just unneccisary code
7th Apr 2022, 1:01 PM
Slick
Slick - avatar
+ 1
[1,2,3] is a list type. I took away the conversion to a list and it works fine https://code.sololearn.com/cKyW4krcUEdl/?ref=app
7th Apr 2022, 12:45 PM
Slick
Slick - avatar
+ 1
No worries. What exactly do you mean by: "a list object is initialized as an attribute for what's already a list object"? Say you have a string: "hello" if you print the string version of that, print(str("hello")) then it just prints the string cause it's already a string
7th Apr 2022, 12:53 PM
Slick
Slick - avatar
+ 1
Its redundant in python altogether. The methods preceded with an underscore in python are only IMPLIED to be private. But I'd need to see the lesson to fully understand what SL was trying to teach.
7th Apr 2022, 1:07 PM
Slick
Slick - avatar
+ 1
Slick Yeah, they actually said in the course that one cannot be private in Python and the "weak" single underscores can be used to prevent importing the dunder methods only, IIRC(edit: checked, and not dunder but singke undercores, again) . Thank you though. The whole code pushes a number into the list and then prints, and then pops the last one and prints, and then formats the whole thing into a string form by defining a __repr__(self)... Because I haven't gotten down any of it, I cannot explain any further. I'd just assumed this much would suffice to inquire about the choice of list as attribute. Thank you so much for your help.
7th Apr 2022, 1:28 PM
KorkunƧ el Gato
KorkunƧ el Gato - avatar
0
Slick May I ask why a list object is initialized as an attribute for what's already a list object? Sorry if I've misused some words here, this is new to me.
7th Apr 2022, 12:48 PM
KorkunƧ el Gato
KorkunƧ el Gato - avatar
0
Slick oh, but do you ever call str() on an actual string? lol I'm the staring chimp again
7th Apr 2022, 12:54 PM
KorkunƧ el Gato
KorkunƧ el Gato - avatar
0
Slick So would you say that SL used such an example (while explaining single underscored methods, privacy, etc) just to be easy on the eyes and it's, ever without a purpose, redundant? I am just trying to make sure to get it right.
7th Apr 2022, 1:05 PM
KorkunƧ el Gato
KorkunƧ el Gato - avatar