Isaac0427 said:
"abc".upper only produces an error message.
Does it? I get
<built-in method upper of str object at 0x7f92a240e7d8>, which is the string representation of the function.
Isaac0427 said:
I still don't understand why the code is not upper("i want this in all caps!").
Because upper is a method of the string object. So the "s.upper()" should be read as "call the upper method of the object s". What would the upper method belonging to a string do except return the upper case version of that string?
Isaac0427 said:
I just don't get the concept, and what the difference between acting on an object (or having the object in the argument) and belonging to an object is.
Methods belong to a class or an object. When you call it, it usually does something with the object.
This is all code organisation tools, that's all. Imagine you are writing a simulation of a solar system. For each planet you need its x, y and z position, x, y and z velocity, and its mass. You could create arrays called x, y, z, vx, vy, vz and M. But that's a weird thing to do - you are simulating planets, but your data organisation collects the x coordinates into one group, the y coordinates in another, and so on. Creating a "planet" class that stores the coordinates, velocity and mass of a planet in one "box" and then creating an array of planets let's you organise the data in a way that reflects how you think about what you are modelling. OK so far?
Now when you come to write the simulation you're going to need a function that calculates the new position of each planet. It'll need to know the positions of all the planets, and which one it's supposed to be updating. Since the function is always going to manipulate the coordinates and velocities of a planet, why not put the function in the same "box" as the data for that planet? Then you don't need to pass the planet object as an argument. The function will know which planet it's supposed to update - the one it's a member of. And in python (and C++, C#, Java, Javascript, VB, and others) the way you say "I want to call the function f that's inside object o" is o.f().
Hope that makes some sense.