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

  • Thread starter Sullivan WxPyQtKinter
  • Start date
S

Sullivan WxPyQtKinter

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"?
 
F

Fredrik Lundh

Sullivan WxPyQtKinter said:
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
Traceback (most recent call last):
Traceback (most recent call last):
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>
 
S

Sullivan WxPyQtKinter

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:'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:'' #well, this is
understandable.'a'

How do you explain this result?


Sincerely, thank you so much for help.
 
F

Fredrik Lundh

Sullivan WxPyQtKinter said:
More confusing things came out to me:
'' #well, this is
understandable.
'a'

How do you explain this result?

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

if you meant to write
'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>
 
D

Dennis Lee Bieber

However, something else came out just now:
'b'

.join() says to "join" the arguments, using (in this case) instr as
the separator. Since you only supplied a single item, there is nothing
to "join", and the single item was returned.
'bac'


'A'

Looks like something flaky in your system...
being changed. So why should it be programmed into the str type?
As an inverse of string.split() -- otherwise you have to the strange
situation of a string based join that is a method of lists, tuples, and
who knows what...

Would you really want to see:

["b", "c"].join("a")

what do you do with

[2, "c", 3.14].join("x")

abc = "This is some string"
sabc = abc.split(" ")
abc == " ".join(sabc)

--
 
R

Rene Pijlman

Sullivan WxPyQtKinter:
Why do the three expression yield the same result "abc"?

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top