Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Confused by Method(function) of a module and method of a class/instance

Reply
Thread Tools

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

 
 
Sullivan WxPyQtKinter
Guest
Posts: n/a
 
      03-07-2006
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"?

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      03-07-2006
"Sullivan WxPyQtKinter" <(E-Mail Removed)> 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>



 
Reply With Quote
 
 
 
 
Sullivan WxPyQtKinter
Guest
Posts: n/a
 
      03-07-2006
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.

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      03-07-2006
"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>



 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      03-07-2006
On 6 Mar 2006 23:18:09 -0800, "Sullivan WxPyQtKinter"
<(E-Mail Removed)> declaimed the following in comp.lang.python:



> However, something else came out just now:
> >>>instr='a'
> >>>instr.join('b')

> '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.

>>> inst = "a"
>>> inst.join("b")

'b'
>>> inst.join(("b", "c"))

'bac'


> >>>instr.lower()

> 'A'


Looks like something flaky in your system...

>>> inst.lower()

'a'
>>> inst

'a'
>>>


> 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)

--
> ================================================== ============ <
> http://www.velocityreviews.com/forums/(E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (E-Mail Removed) | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

 
Reply With Quote
 
Rene Pijlman
Guest
Posts: n/a
 
      03-07-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to exec a module method when "include Module" from a Class ? Iñaki Baz Castillo Ruby 3 05-02-2008 09:37 AM
Mixin a module method as a class method Farrel Lifson Ruby 9 10-19-2006 12:06 PM
Re: module docstring, documentation,anything? please note is the module type/object NOT some module Maric Michaud Python 0 06-24-2006 12:42 PM
Confused with module and .py files Iyer, Prasad C Python 2 10-05-2005 12:44 PM
Should every method have a module method? Trans Ruby 7 02-01-2005 01:37 PM



Advertisments