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

Discussion in 'Python' started by Sullivan WxPyQtKinter, Mar 7, 2006.

  1. 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"?
    Sullivan WxPyQtKinter, Mar 7, 2006
    #1
    1. Advertising

  2. Re: Confused by Method(function) of a module and method ofaclass/instance

    "Sullivan WxPyQtKinter" <> 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>
    Fredrik Lundh, Mar 7, 2006
    #2
    1. Advertising

  3. Re: Confused by Method(function) of a module and method of aclass/instance

    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.
    Sullivan WxPyQtKinter, Mar 7, 2006
    #3
  4. Re: Confused by Method(function) of a module and methodofaclass/instance

    "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>
    Fredrik Lundh, Mar 7, 2006
    #4
  5. Re: Confused by Method(function) of a module and method of aclass/instance

    On 6 Mar 2006 23:18:09 -0800, "Sullivan WxPyQtKinter"
    <> 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)

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
    Dennis Lee Bieber, Mar 7, 2006
    #5
  6. Sullivan WxPyQtKinter

    Rene Pijlman Guest

    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
    Rene Pijlman, Mar 7, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sridhar R
    Replies:
    14
    Views:
    1,381
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  2. David Garamond
    Replies:
    5
    Views:
    215
    Ara.T.Howard
    Jun 8, 2004
  3. Replies:
    1
    Views:
    135
    Trans
    Feb 5, 2007
  4. Raj Singh
    Replies:
    2
    Views:
    186
    Rick DeNatale
    May 29, 2008
  5. Greg Hauptmann
    Replies:
    9
    Views:
    231
    Loren Segal
    Jun 16, 2008
Loading...

Share This Page