I have a list...

Discussion in 'Python' started by Damir Hakimov, Jul 1, 2003.

  1. Hi, All!

    say, i have a function:

    def f(*b):
    print b
    return

    then i do:
    f(3,4,5)
    (3, 4, 5)

    but i have list f=(3,4,5)
    f(l)
    ((3, 4, 5),)

    how can i call f function to result
    f(???(b))
    (3, 4, 5)

    Thanks!
     
    Damir Hakimov, Jul 1, 2003
    #1
    1. Advertising

  2. On Tue, 1 Jul 2003 11:39:15 +0400, Damir Hakimov <> wrote:
    > Hi, All!
    >
    > say, i have a function:
    >
    > def f(*b):
    > print b
    > return
    >
    > then i do:
    > f(3,4,5)
    > (3, 4, 5)
    >
    > but i have list f=(3,4,5)
    > f(l)
    > ((3, 4, 5),)
    >
    > how can i call f function to result
    > f(???(b))
    > (3, 4, 5)


    You mean apply(f,(3,4,5)) ?


    Albert
    --
    Unlike popular belief, the .doc format is not an open publically available format.
     
    Albert Hofkamp, Jul 1, 2003
    #2
    1. Advertising

  3. Damir Hakimov

    Guest

    Damir Hakimov wrote:
    > Hi, All!
    >
    > say, i have a function:
    >
    > def f(*b):
    > print b
    > return
    >
    > then i do:
    > f(3,4,5)
    > (3, 4, 5)
    >
    > but i have list f=(3,4,5)
    > f(l)
    > ((3, 4, 5),)
    >
    > how can i call f function to result
    > f(???(b))
    > (3, 4, 5)
    >
    > Thanks!
    >
    >
    >

    You can use the keyword 'type' to check the type of your arguments
    and return the appropriate 'format' according to the their types

    Regards

    Salvatore
     
    , Jul 1, 2003
    #3
  4. Damir Hakimov

    Dialtone Guest

    "Damir Hakimov" <> writes:

    > say, i have a function:
    >
    > def f(*b):
    > print b
    > return
    >
    > then i do:
    > f(3,4,5)
    > (3, 4, 5)


    This is not a list but a tuple.

    > but i have list f=(3,4,5)
    > f(l)
    > ((3, 4, 5),)


    The standard way to represent a tuple with one element is to put a coma
    after that element like ("donald",)

    > how can i call f function to result
    > f(???(b))
    > (3, 4, 5)


    If you want this you should use a list which has square brackets [].

    But the arguments passed with *b are incapsuleted into a tuple so you should
    print something like this:

    >>> def f(*b):

    .... print b[0]

    >>> f([1,2,3])

    [1, 2, 3]



    --
    try: troll.uses(Brain)
    except TypeError, data:
    troll.plonk()
    Linux User #310274, Debian Sid Proud User
     
    Dialtone, Jul 1, 2003
    #4
  5. Damir Hakimov

    Duncan Booth Guest

    "Damir Hakimov" <> wrote in
    news::

    > Hi, All!
    >
    > say, i have a function:
    >
    > def f(*b):
    > print b
    > return
    >
    > then i do:
    > f(3,4,5)
    > (3, 4, 5)
    >
    > but i have list f=(3,4,5)
    > f(l)
    > ((3, 4, 5),)
    >
    > how can i call f function to result
    > f(???(b))
    > (3, 4, 5)
    >

    I'm not sure any of the other responses actually answered the question,
    which I think was meant to be, given a tuple l=3,4,5 how do you pass that
    tuple to the function f so that b simply gets the tuple. The answer is that
    you try:

    >>> f(*l)

    (3,4,5)

    If that doesn't work, then you upgrade to a more recent version of Python.
    If you (or your users) really can't upgrade you should use 'apply'.

    --
    Duncan Booth
    int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
    "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
     
    Duncan Booth, Jul 1, 2003
    #5
  6. Damir Hakimov

    Aahz Guest

    In article <>,
    Damir Hakimov <> wrote:
    >
    > [...]


    ....it never will be missed.
    --
    Aahz () <*> http://www.pythoncraft.com/

    Apologies to G&S and everyone reading this.
     
    Aahz, Jul 1, 2003
    #6
  7. On Tue, 1 Jul 2003 11:39:15 +0400, "Damir Hakimov" <> wrote:

    >Hi, All!
    >
    >say, i have a function:
    >
    >def f(*b):
    > print b
    > return
    >
    >then i do:
    >f(3,4,5)
    >(3, 4, 5)
    >
    >but i have list f=(3,4,5)
    >f(l)
    >((3, 4, 5),)
    >
    >how can i call f function to result
    >f(???(b))
    >(3, 4, 5)
    >

    Is this what you are looking for? :

    >>> def f(*b):

    ... print b
    ...
    >>> tup = (1,2,3)
    >>> f(tup)

    ((1, 2, 3),)

    tup was single arg, but:

    >>> f(*tup)

    (1, 2, 3)

    tup got unpacked to make args

    >>> L = [4,5,6]
    >>> f(L)

    ([4, 5, 6],)

    L was single arg, but:

    >>> f(*L)

    (4, 5, 6)

    L got unpacked similarly, but note that args become tuple b, not a list.

    Regards,
    Bengt Richter
     
    Bengt Richter, Jul 2, 2003
    #7
    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. =?Utf-8?B?cmdyYW5kaWRpZXI=?=

    Can I have base class derived from System.Web.UI.Page have design

    =?Utf-8?B?cmdyYW5kaWRpZXI=?=, Jul 22, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    484
    Patrice
    Jul 22, 2005
  2. okaminer
    Replies:
    3
    Views:
    307
    okaminer
    Aug 1, 2005
  3. =?Utf-8?B?ZGF2aWQ=?=
    Replies:
    6
    Views:
    5,498
    Kevin Spencer
    Aug 18, 2005
  4. =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=

    Does a resource have to have all phrases in it?

    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=, Jan 30, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    378
    Neil Ni
    Feb 3, 2006
  5. Mufasa
    Replies:
    0
    Views:
    364
    Mufasa
    Dec 2, 2007
Loading...

Share This Page