I have tried and errored a reasonable amount of times
Cameron Simpson
cs at zip.com.au
Sun Aug 31 01:30:26 EDT 2014
On 30Aug2014 17:48, Seymore4Head <Seymore4Head at Hotmail.invalid> wrote:
>I have been told that one is a method and the other calls a method. I
>still have to learn exactly what that means. I'm getting there.
A method is, essentially, a function. Observe:
def my_func(x):
print(9)
my_func is just the name of the function, and .isupper is likewise just the
name of the function that tests a string for uppercaseness.
Conversely, my_func() actually calls the function, and likewise .isupper()
calls the test function, returning True or False depending on whether the
string was uppercase or not.
A method versus a function? A method is a particular type of function. It is
normally defined in a class, eg:
class MyClass:
def method_name_here(self, arg1, arg2):
... do something with self and arg1 and arg2 ...
When you have an object which is an instance of the class (let us call it "o"),
when you call:
o.method_name_here(1,2)
it invokes the function MyClass.method_name_here(o,1,2). So because the string
"no" is an instance of str, the code:
"no".isupper()
runs the function str.isupper("no"), which examines its argument for
uppercaseness.
Cheers,
Cameron Simpson <cs at zip.com.au>
Why is it so hard for people to simply leave people alone? But, the answer
comes to me: they are idiots and in a perfect world, I would be permitted to
kill them all. - Julie Rhodes <jk.rhodes at asacomp.com>
More information about the Python-list
mailing list