469,290 Members | 1,882 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

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

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

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?

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

Mar 7 '06 #1
4 1319
"Sullivan WxPyQtKinter" <su***********@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>

Mar 7 '06 #2
Yes, I checked out that I have already run "from string import *". So
the lower() means string.lower() function.

However, something else came out just now:
instr='a'
instr.join('b') 'b'instr.lower() 'A'instr 'a'
Both as the method of the type str, join never use the instr instance
object as method parameters while lower do. Compared with the .lower()
method, the instr.join() looks like an independent function, which use
the parameters in the parenthesis and return a value without instr
being changed. So why should it be programmed into the str type?

More confusing things came out to me:str().lower() '' #well, this is
understandable.str.lower(str(),'A')

'a'

How do you explain this result?
Sincerely, thank you so much for help.

Mar 7 '06 #3
"Sullivan WxPyQtKinter" wrote:
More confusing things came out to me:
str().lower() '' #well, this is
understandable.str.lower(str(),'A') 'a'

How do you explain this result?


I get:
str.lower(str(), 'A') Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: lower() takes no arguments (1 given)

if you meant to write
str.lower('A')

'a'

it's because "str" is the class that implements methods for the "str" type,
and, for an instance I of the class C:

I.method()

and

C.method(I)

are, in general, the same thing in Python

</F>

Mar 7 '06 #4
Sullivan WxPyQtKinter:
Why do the three expression yield the same result "abc"?


Because all three converted "ABC" to lowercase, as per your request.

--
René Pijlman
Mar 7 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by OKB (not okblacke) | last post: by
5 posts views Thread by Shapper | last post: by
2 posts views Thread by The Mess | last post: by
1 post views Thread by Pankaj | last post: by
10 posts views Thread by Matthew | last post: by
6 posts views Thread by mh | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.