Confused by Method(function) of a module and method of aclass/instance

Fredrik Lundh fredrik at pythonware.com
Tue Mar 7 07:52:06 CET 2006


"Sullivan WxPyQtKinter" <sullivanz.pku at gmail.com> wrote:

> In python, these expression seems yields the same result:
>
> inputstring='ABC'
>
> print inputstring.lower()
> print lower(inputstring)
> print string.lower(inputstring)
>
> result:
> abc
> abc
> abc

I get

>>> inputstring="ABC"
>>> print inputstring.lower()
abc
>>> lower(inputstring)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'lower' is not defined
>>> string.lower(inputstring)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'string' is not defined

> Question:
> Is the method lower() just a method for the inputstring instance( an
> instrance object of a string class object), or a function in the module
> string.py or a build-in function or sth else?

inputstring.lower() refers to the method "lower" of the "inputstring"
object.

string.lower() refers to the function "lower" in the "string" module.

what "lower" refers to isn't clear, but I assume that you've done "from
string import *" or something like that earlier on, which means that it's
just an alias for the function "lower" in the "string" module.

> Why do the three expression yield the same result "abc"?

because the lower method and the lower function and the string.lower
function happens to do the same thing ?

that doesn't mean that all methods/functions with the same name do
the same thing, of course: shutil.copy() and dict().copy() are two en-
tirely different things, for example.

</F>






More information about the Python-list mailing list