Very simple request about argument setting.

Discussion in 'Python' started by Hakusa@gmail.com, Oct 30, 2006.

  1. Guest

    I have the argument items in my class room.

    class room:
    def __init__(self, name, description, items*):

    I thought I remembered from a tutorial I read once, and I've read so
    many I feel like an expert of them, that putting a little star* above
    an item makes it accept the argument as a list. But when I tried this,
    I got an invalid syntax error message.

    So:
    Question 1: How can I create an argument that accepts a list of
    variable?

    Question 2: How do I signify to accept each item as one list? (my bet's
    on using another set of parens)

    Question 3: Or am I going about this all wrong and should always create
    a list before the fuction call and use the list in the function call?
     
    , Oct 30, 2006
    #1
    1. Advertising

  2. ArdPy Guest

    wrote:
    > I have the argument items in my class room.
    >
    > class room:
    > def __init__(self, name, description, items*):
    >
    > I thought I remembered from a tutorial I read once, and I've read so
    > many I feel like an expert of them, that putting a little star* above
    > an item makes it accept the argument as a list. But when I tried this,
    > I got an invalid syntax error message.
    >

    There is an error in the syntax the star must prefix the variable name
    not suffix it.
    Then the items variable will accept the parameter value as a tuple.
    > So:
    > Question 1: How can I create an argument that accepts a list of
    > variable?

    It is not clear whether you want to accept a list variable or an
    arbitrary number of values as parameter
    to the function. So I will explain each case.
    def ex(name,*items):
    Here name, description and items can all accept lists as arguments.
    Additionally items can accept arbitrary
    number of arguments even of different types.
    Ex:
    ex([10,12],12,13.3,'Python')
    >
    > Question 2: How do I signify to accept each item as one list? (my bet's
    > on using another set of parens)

    I have answered this already.
    >
    > Question 3: Or am I going about this all wrong and should always create
    > a list before the fuction call and use the list in the function call?

    No need u can pass a list directly into the call.
     
    ArdPy, Oct 30, 2006
    #2
    1. Advertising

  3. Guest

    ArdPy wrote:
    > There is an error in the syntax the star must prefix the variable name
    > not suffix it.
    > Then the items variable will accept the parameter value as a tuple.


    Hmm, tuples are immutable, right? I need something mutable so that when
    the player picks up an item, it's no longer in the room.

    Besides which, I've been playing with this whole thing in the
    interactive interpreter:

    >>> def shuf(x,*foo, **bar):

    .... return x, foo
    ....
    >>> x,foo,bar = shuf(1,2,3,4,5,6,7,8)

    Traceback (most recent call last):
    File "<interactive input>", line 1, in <module>
    ValueError: need more than 2 values to unpack

    I remember that two stars is for the second tuple, but how do I
    distinguish between what var is for which tuple? I've tried using
    brackets, braces, prenthisies, nothing seems to work. So what I am
    doing wrong?
     
    , Oct 30, 2006
    #3
  4. ArdPy Guest

    wrote:
    > ArdPy wrote:
    > > There is an error in the syntax the star must prefix the variable name
    > > not suffix it.
    > > Then the items variable will accept the parameter value as a tuple.

    >
    > Hmm, tuples are immutable, right? I need something mutable so that when
    > the player picks up an item, it's no longer in the room.
    >
    > Besides which, I've been playing with this whole thing in the
    > interactive interpreter:
    >
    > >>> def shuf(x,*foo, **bar):

    > ... return x, foo
    > ...
    > >>> x,foo,bar = shuf(1,2,3,4,5,6,7,8)

    > Traceback (most recent call last):
    > File "<interactive input>", line 1, in <module>
    > ValueError: need more than 2 values to unpack
    >
    > I remember that two stars is for the second tuple, but how do I
    > distinguish between what var is for which tuple? I've tried using
    > brackets, braces, prenthisies, nothing seems to work. So what I am
    > doing wrong?

    The thing is that the parameter 'bar' is actually a dictionary object
    so the right way of calling shuf is then
    x,foo,bar = shuf(1,2,3,4,5,a=6,b=7,c=8)
     
    ArdPy, Oct 30, 2006
    #4
  5. Guest

    Thank you very much for your information.
     
    , Oct 30, 2006
    #5
  6. wrote:

    > ArdPy wrote:
    >> There is an error in the syntax the star must prefix the variable name
    >> not suffix it.
    >> Then the items variable will accept the parameter value as a tuple.

    >
    > Hmm, tuples are immutable, right? I need something mutable so that when
    > the player picks up an item, it's no longer in the room.


    the *args syntax is used to capture extra positional arguments, so you
    can deal with them later. consider the following code:

    def func(*args):
    args.append(4)

    func(1, 2, 3)

    where do you expect the "4" to go?

    if you want to *store* the items in an *instance* variable, you can
    trivially convert it to a list by passing it to the list() function:

    class Room:
    def __init__(self, name, *items):
    self.name = name
    self.items = list(items)

    r = Room("hall", "old shoe", "kitten", "vegetables")
    r.items.append("a wallclock round in metal")

    > Besides which, I've been playing with this whole thing in the
    > interactive interpreter:
    >
    >>>> def shuf(x,*foo, **bar):

    > ... return x, foo
    > ...
    >>>> x,foo,bar = shuf(1,2,3,4,5,6,7,8)

    > Traceback (most recent call last):
    > File "<interactive input>", line 1, in <module>
    > ValueError: need more than 2 values to unpack


    if you compare the return statement inside the function with the
    assignment, do you see any notable differences?

    > I remember that two stars is for the second tuple


    if you're an expert on tutorials, surely you should be able to find one
    and look this up in no time at all?

    (the **bar syntax captures extra *keyword* arguments)

    > So what I am doing wrong?


    programming by trial and error?

    </F>
     
    Fredrik Lundh, Oct 30, 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. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    509
    Raymond Hettinger
    Jul 27, 2003
  2. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,697
    Merrill & Michele
    Oct 19, 2004
  3. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    451
    jacob navia
    Oct 17, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    531
    Dave Thompson
    Feb 14, 2005
  5. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,350
Loading...

Share This Page