473,659 Members | 2,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

method (a, b = '', *c, **d): gets a syntax error?

Hello.

I am relatively new to Python and I have a strange problem with some
code. In a class the __call__ method gets parameters like this:
class WhatsUp:
__call__ (
self,
var1,
var2 = '',
*moreVars,
**moreNamedVars ,
):
pass
I always get an error for the **moreNamedVars , line where the '^' points
at the comma at the end (or, if I remove the comma, it points at the
colon). I also tried commenting-out the other variables passed to the
method. Same result.
I assumed the order of variables and variable lists of variables had to
be like this? What is wrong?
Kind regards

Andreas

Jul 18 '05 #1
11 2831
Andreas Neudecker wrote:
Hello.

I am relatively new to Python and I have a strange problem with some
code. In a class the __call__ method gets parameters like this:
class WhatsUp:
__call__ (


You're missing the keyword 'def' here, right before the method name.
Alex

Jul 18 '05 #2
Andreas Neudecker <a.*********@un i-bonn.de> writes:
Hello.

I am relatively new to Python and I have a strange problem with some
code. In a class the __call__ method gets parameters like this:
class WhatsUp:
__call__ (
self,
var1,
var2 = '',
*moreVars,
**moreNamedVars ,
):
pass
I always get an error for the **moreNamedVars , line where the '^'
points at the comma at the end (or, if I remove the comma, it points
at the colon). I also tried commenting-out the other variables passed
to the method. Same result.
I assumed the order of variables and variable lists of variables had
to be like this? What is wrong?


Missing a def?

Cheers,
mwh

--
Any form of evilness that can be detected without *too* much effort
is worth it... I have no idea what kind of evil we're looking for
here or how to detect is, so I can't answer yes or no.
-- Guido Van Rossum, python-dev
Jul 18 '05 #3

"Andreas Neudecker" <a.*********@un i-bonn.de> wrote in message
news:3F******** ******@uni-bonn.de...
class WhatsUp:
__call__ (
self,
var1,
var2 = '',
*moreVars,
**moreNamedVars ,
):
pass
I always get an error for the **moreNamedVars , line where the '^' points at the comma at the end (or, if I remove the comma, it points at the
colon). ...What is wrong?


Generaly, when reporting 'I got an error', you should copy the actual
error message. In this case, I presume you got 'SyntaxError: invalid
syntax' for each of your two syntax errors.

1. When you make a function call and use **whatever, it must be the
last item in the argument list, just as in a function definition. A
following comma is not allowed for either defs or calls. So when you
add 'def' to correct your actual error, you also need to omit the
comma.

2. The colon suffix is required for defs but forbidden for calls.
With the comma present, the parser never got far enough to this.
Unlike most batch compilers, the CPython parser quits at the first
error it sees. So when you correct one error and resubmit, you may
expose another further in your code.

Welcom to Python. Enjoy.

Terry J. Reedy
Jul 18 '05 #4
Hi.

Terry Reedy wrote:
Generaly, when reporting 'I got an error', you should copy the actual
error message.
Will do from now. Thanks.
In this case, I presume you got 'SyntaxError: invalid
syntax' for each of your two syntax errors.
Exactly. Got one. As Michael Peuser states, the comma behind the last
parameter is fine. I am doing this all the time when the list is long
and I write it one parameter per line, because it makes swapping order
or adding another parameter less error-prone (how often have you
forgotten to add the comma to the previous parameter when adding a new
'last one').
1. When you make a function call and use **whatever, it must be the
last item in the argument list, just as in a function definition.
I knew that. That was what puzzled me. And the pointer of the error
message pointed to the comma. So I simply overlooked that I had
forgotten the 'def' - what a dumb bug!
Welcom to Python. Enjoy.


I do. Used to do some C long, long ago. Python is so much more
comfortable ...

Regards
Andreas

Jul 18 '05 #5
Sorry, have to correct myself on the comma topic:
class WhatsUp:
# No comma at the end of the list. This works fine.
def method1 (
self,
var1,
var2 = '',
*more,
**moreNamed
):
print "This works fine."

# This also works fine, even with the comma at the end.
def method2 (
self,
var1,
var2 = '',
var3 = '', # <- THIS COMMA IS FINE.
):
print "This works fine."

# This will raise a Syntax error because of the comma.
def method3 (
self,
var1,
var2 = '',
*more,
**moreNamed, # <- THIS COMMA IS NOT ALLOWED.
):
print "The comma at the end of the parameter list will \
raise a syntax error."
So to me it looks like it makes a difference whether the list contains a
variable parameter list or not. I use the version as in method2 often
for reasons stated in my previous posting.

Still it puzzles me that in one case it is okay in the other one not.
Seems unlogical to me. Can anyone enlighten me to why this is so?

Thanks to all of you who bothered to point me to my stupid error of
forgetting the 'def' and discussing with me the comma problem.
Kind regards
Andreas
Jul 18 '05 #6

"Michael Peuser" <mp*****@web.de > wrote in message
news:bi******** *****@news.t-online.com...

"Terry Reedy" <tj*****@udel.e du> schrieb im Newsbeitrag
news:3d******** ************@co mcast.com...

[...}

1. When you make a function call and use **whatever, it must be the
last item in the argument list, just as in a function definition. A following comma is not allowed for either defs or calls.
This in fact is not true. Funnily you can add *one* comma at the end

of any list-like construct.


For 2.2.1 and whatever version Andreas is running, **whatever is an
exception and CANNOT be followed by a comma in either def or call,
just as I said. I tested before writing. (Did you? Can you test
below on 2.3?)
def f(**d): .... for i,v in d.items(): print i, v
.... b={'one':1, 'two':2}
f(**b) two 2
one 1 f(**b,) File "<stdin>", line 1
f(**b,)
^
SyntaxError: invalid syntax
def f2(**d,): pass

File "<stdin>", line 1
def f2(**d,): pass
^
SyntaxError: invalid syntax

# On the original fixed-pitch font screen, both arrows point at
offending comma.

Can someone verify this for 2.3? If so, there is a bug in either doc
or interpreter.

Terry J. Reedy
Jul 18 '05 #7
Terry Reedy wrote:
def f2(**d,): pass

File "<stdin>", line 1
def f2(**d,): pass
^
SyntaxError: invalid syntax

# On the original fixed-pitch font screen, both arrows point at
offending comma.

Can someone verify this for 2.3? If so, there is a bug in either doc
or interpreter.


Python 2.3 does the same.

Python 2.3 (#1, Aug 5 2003, 14:13:25)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
0 >>> zip(**(),)
File "<stdin>", line 1
zip(**(),)
^
SyntaxError: invalid syntax
1 >>> def foo(**a,): pass
File "<stdin>", line 1
def foo(**a,): pass
^
SyntaxError: invalid syntax

yours,
Gerrit.

--
216. If the patient be a freed man, he receives five shekels.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
http://www.sp.nl/

Jul 18 '05 #8
Andreas Neudecker <a.*********@un i-bonn.de> writes:

[comma inconsistencies]
Still it puzzles me that in one case it is okay in the other one
not. Seems unlogical to me. Can anyone enlighten me to why this is so?
Because that's what the grammar says. The chances of this being
deliberate are, I would hazard, pretty tiny.
Thanks to all of you who bothered to point me to my stupid error of
forgetting the 'def' and discussing with me the comma problem.


If it's any consolation, it took quite a bit of staring at it before I
noticed the flaw...

Cheers,
mwh

--
I located the link but haven't bothered to re-read the article,
preferring to post nonsense to usenet before checking my facts.
-- Ben Wolfson, comp.lang.pytho n
Jul 18 '05 #9
>>>>> Andreas Neudecker <a.*********@un i-bonn.de> (AN) wrote:

AN> Still it puzzles me that in one case it is okay in the other one not. Seems
AN> unlogical to me. Can anyone enlighten me to why this is so?

It is quite logical. The idea behind the optional comma after the last
parameter is such that it would be easy to change your function to include
additional parameters. When you put each parameter on a separate line (as
you did in your examples) , you just have to add a single line with the new
parameter and it doesn't matter whether the new parameter comes in as the
last one or at any other place. If you leave out the final comma, and the
new parameter is the last one, you would have to add the comma to the
previous line, which can easily be forgotten.

However, when you have **kwargs it must be last, so there is no need to
put an additional comma. In fact it would be confusing as it would suggest
that you can add additional parameters after it.
--
Piet van Oostrum <pi**@cs.uu.n l>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.***********@h ccnet.nl
Jul 18 '05 #10

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

Similar topics

3
6222
by: Robert Mark Bram | last post by:
Hi All! I have the following two methods in an asp/jscript page - my problem is that without the update statement there is no error, but with the update statement I get the following error: Error Type: Microsoft OLE DB Provider for ODBC Drivers (0x80004005) Syntax error in UPDATE statement. /polyprint/dataEntry.asp, line 158
6
1706
by: Euripides J. Sellountos | last post by:
Hello kind people. I hope you can you help me with the following problem. The following snippet fails to compile with g++. (It compiles fine with other compilers.) All I want to do is to throw an "error" exception when the constructor A::A() gets called. I don't understand. Do i really have any syntax error? I'm receiving the following error message
4
5318
by: Bob Stearns | last post by:
The statement: merge into nullid.animals_et_in t1 using is3.animals t2 on t1.sire_assoc=t2.assoc and t1.sire_prefix=t2.prefix and t1.sire_regnum=t2.regnum when matched then update set t1.sire_bhid=t2.bhid when not matched then update set t1.sire_bhid=0 go gets the error message:
4
3399
by: Peter | last post by:
Hello, Thanks for reviewing my question. I am just starting out create a custom control following the KB example "Create a Data Bound ListView Control". I am receiving a syntax error on the following: (43): No overload for method 'EditorAttribute' takes '1' arguments (43): No overload for method 'GetType' takes '1' arguments // << SYNTAX ERROR
2
4672
by: J W | last post by:
I have an ASP page in which there is embedded SQL. I am trying to use variables in the SQL but am getting the error 'syntax error or access violation'. In the past, I have successfully used variables for table names and within WHERE clauses in SQL in an ASP page. This time, I am trying to use variables in the SELECT portion of an INSERT statement. (1) The following code works in a SQL stored procedure: DECLARE @BatchId uniqueidentifier...
0
1777
by: Stef Mientki | last post by:
hello, I've syntax error which I totally don't understand: ########## mainfile : import test_upframe2 if __name__ == '__main__': var1 = 33 code = 'print var1 + 3 \n'
0
2996
by: Terry Reedy | last post by:
Stef Mientki wrote: Indendation is screwed. Is the above all do_more body? Which locals does this get you? __init__'s? (locals()?) Isn't this just the same as globals()?
13
3785
Topbidder
by: Topbidder | last post by:
I have this error on the code Parse error: syntax error, unexpected '"' in /home/topbidd/public_html/bid2/bid_classic.php on line 159 now i thought the error was this It seems that the code has an extra " at the end before the ; VALUES('" .$auction_id. "','" .$user_id. "','" .converttonum(get_next_bid($auction_id)). "','" .$NOW. "')";
5
1928
by: Adam Pelling | last post by:
I'm getting this error Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in /home/neblncbt/public_html/forum/includes/acp/acp_board.php on line 69 Here is the acp_board.php file <?php /** * * @package acp
0
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
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...
0
8627
tracyyun
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...
0
7356
agi2029
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...
0
5649
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();...
0
4175
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...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
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
2
1737
bsmnconsultancy
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...

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.