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? 5 1242 Ha****@gmail.co m 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,'Pytho n')
>
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 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 "<interacti ve 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? Ha****@gmail.co m 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 "<interacti ve 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)
Thank you very much for your information. Ha****@gmail.co m 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 "<interacti ve 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> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 separation.
Having done a few searches, I've come across the concept of 'template
engines', but that seems like overkill for my needs. I'm thinking I'll
just stick with php as my template language; a little embedded code in
the view pages, for loops or whatever, should be ok..
I've come up with the...
|
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:
http://MyServer.com/Update.asp?UserName=Tom
My asp code is like this:
%@ Language=VBScript %>
<!--#include file="includes/openconnection.asp"-->
|
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.
var edition = Request.Form ("edition");
var language = Request.Form ("language");
Response.Write("Edition is type "" + (typeof edition) + "" and
value "" + edition + ""<br>");
|
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
ideas - its not vital but would make the code a lot nicer to read:
#define SET(x,y) (x |=(1<<y))
#define PIEZO PORTB,5
|
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
encoding, I set the Response.Charset and Response.ContentEncoding to the
encoding of current content. Furthermore I set the Content-Type META tag of
the html page to use the same charset as well.
And this all works fine... I get my pages served and both the HTTP headers
| |
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 can
find any answer. Please can someone help me?
The following code is a complete page that demonstrates my problem. If
you save this as an .aspx and load it in a browser, it tells you it is
creating the cookie. If you reload the page, it tells you it is changing
the value. If you reload it...
|
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?).
I'm using Prototype 1.4 and the my works great with Firefox,but with
IE6 the onFailure never gets called and the request never completes.
My code:
var ajaxReq = new Ajax.Request( url, {method: 'post', parameters:
|
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 programs" Axel Simon tries to establish a
theoretical framework for analyzing C programs. In contrast to other
books where the actual technical difficulties are "abstracted away",
this books tries to analyze real C programs taking into account
pointers, stack frames, etc.
|
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 timeout like after 100 seconds with a message?.
Heres my code:
var xmlhttp = false ;
if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
{
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |