472,348 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,348 software developers and data experts.

Very simple request about argument setting.

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 '06 #1
5 1169

Ha****@gmail.com 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.

Oct 30 '06 #2

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 '06 #3

Ha****@gmail.com 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)

Oct 30 '06 #4
Thank you very much for your information.

Oct 30 '06 #5
Ha****@gmail.com 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>

Oct 30 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Droolboy | last post by:
I'm trying to build a fairly small (max. 10 different pages) site using php, and it's becoming obvious that I need some kind of model view...
13
by: Samantha Smit | last post by:
Hi, I am trying to create a simple asp page that has one command button that updates a database. The URL of the page is like this: ...
8
by: Robert Mark Bram | last post by:
Hi All! I have the following code in an asp page whose language tag is: <%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%> // Find request variables....
14
by: Malcolm | last post by:
Hi, I have the following which fails with "disagreement in number of macro arguments" when compiling with Imagecraft ICCAVR. Has anyone got any...
8
by: Ricky K. Rasmussen | last post by:
Hello NG, I'm having some encoding trouble using my own RequestHandler: Since the RequestHandler must be able to serve content with diffrent...
0
by: Alan Silver | last post by:
Hello, I am having a problem setting and resetting cookies. I'm sure I just doing something really stupid as this is such a basic issue, but I...
5
by: dougwig | last post by:
I'm trying to handle the scenario where a user's session times out and and their ajax request triggers a redirection by the webserver (302 error?)....
126
by: jacob navia | last post by:
Buffer overflows are a fact of life, and, more specifically, a fact of C. All is not lost however. In the book "Value Range Analysis of C...
20
by: Bryan A | last post by:
Is there a way to add a timeout to this script so that it times out at a certain time. So it would be auto updating every 2seconds and it would...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.