Functions, methods, and how many arguments do I have to give them?

  • A+
Category:Languages

Why do the following lines give me the same result?

str.upper('hello') 

and

'hello'.upper() 

I tried to do the same with list.append but got a TypeError.

list.append([1])  

Is the str type in python overloaded? How can this be achieved by writing a class/function? I would appreciate an example.

Thanks,

RP.

 


list.append takes two arguments - the list to modify and the element to append. So you need to do it like this:

ls = [1] list.append(ls, 2) 

which is equivalent to the much more popular:

ls.append(2) 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: