Syntax error at function definition

I’ve met the Syntax error when calling the copied code from the exiting Git code below.

File "", line 1 def aaa(aaa, *, bbb=1, ccc=60): _____________^______________

Seeing the message, something may be wrong around " * " .
Is there any problem with using " * " at function definition ?
Or any enviromental conditon, restriction to use this ? (Python version or any library or enviromental variable…)

Are you using splat as a prefix to an argument list? It should be attached to the parameter so the interpreter knows to unpack the list.

def foo(*args):
    a, b, c, x, y, z = args
1 Like

Yes, splat is described in the original code in the Git.
When calling this, I’ve met the error message.
So I’m checking this step.

Will need to read up on that operator so as not to mislead you, but I do not believe it can be IN the argument list, since it is an operator, not an object. Your aaa above might well be expected to be, *aaa.

1 Like

Thanks to recommend.
I do not know why the original code described splat and the following parameter separately.
Is there any meaning using splat only as an indepenent parameter ?

May be type mistake in the originral code.

Honestly don’t know Python that intimately to be able to say yea or nay, but I can’t think of anywhere else the operator is used, It is of course the multiplaction operator so we can follow the parallel to multiple objects in a list that all needed to be unpacked to be useful to a function. Python is quite open to using operators, as in, ['O'] * 5 giving ['O','O','O','O','O'].

The range of operators is well documented so if you feel the need, by all means oblige yourself to vet the appropriate docs.

Missed this question…

No, not as a parameter because there would be no name to attach to the list coming in. *args says that args is a list (or tuple) that will need unpacking. * says nothing and if ignored, does nothing. If not, it should raise some kind of exception. But I speak without actually testing, so here goes…

>>> def foo(*):
	return *
SyntaxError: invalid syntax

It follows that if we can pass in an anonymous object we should be able to return its identity. The above proves we cannot return the identity of an operator.

>>> def foo(*a):
	return a

>>> foo(1,2,3,4)
(1, 2, 3, 4)

We can repackage in one stroke…

>>> def foo(*a):
	return [x for x in a]

>>> foo(1,2,3,4)
[1, 2, 3, 4]
1 Like

Thank you for your kindly explanation.
It may be resulted from the type misatek at the original.

I will check the original code and amend it as you recommend.
Then test working properly.

1 Like

Still running seat of the pants experiments…

>>> def bar(*b,*c):
	t = zip(b,c)
	return {k[0]:k[1] for k in t}
SyntaxError: invalid syntax

Seems we cannot have two splats in one parameter list.

Actually, that was not very fitting example, but it did show us something. In cases where unpacking is not needed…

>>> def bar(b,c):
	return {k[0]:k[1] for k in zip(b,c)}

>>> city_teams = bar(('EDM', 'CGY', 'VAN', 'WPG', 'TOR', 'MON', 'OTT'), ['Oilers', 'Flames', 'Canucks', 'Jets', 'Maple Leafs', 'Canadiens', 'Senators'])
>>> print (city_teams)
{'OTT': 'Senators', 'EDM': 'Oilers', 'MON': 'Canadiens', 'TOR': 'Maple Leafs', 'CGY': 'Flames', 'WPG': 'Jets', 'VAN': 'Canucks'}

We can still zip unmatched data structures since one is ordered (the list) and the other is immutable (the tuple). They are both iterable so get along just fine with zip.

They are both indexed objects, though, and I’m not sure what all we can do with a splat arguments list other than for it. But, we’ll have to run off and test some more (which I hope you will find yourself doing in due course).

1 Like

Seeing the originral code, I found the comment as below.

“Wait until everybody has the same tip.”

Splats in this code seems to mean “a temtative reserved/highlight.” operator for future.

Very confusing.
Sorry for bothering you.

No bother, at all. This is something I need to know as well as you so we’re in this together.

To my mind, and according to what’s been demonstrated so far, the meaning is clear… Unpack an arguments list into unique variables that correspond to those arguments.

def triangle_angle_sum(*angles):
    a, b, c = angles
    return a + b + c

print triangle_angle_sum(30, 60, 90)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.