473,769 Members | 5,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Argument Precedence (possible bug?)

Hello all,

I am just learning Python and have come across something I feel might
be a bug. Please enlightenment me... The following code presents a
challenge. How in the world do you provide an argument for *arg4?

## =============== =============== =============== ===============
def argPrecedence(p ar1, par2=0, par3=0, *par4, **par5):
print 'par1 =', par1, ' # positional argument'
print 'par2 =', par2, ' # keyword argument'
print 'par3 =', par3, ' # keyword argument'
print 'par4 =', par4, ' # argument converted to tuple'
print 'par5 =', par5, ' # argument converted to dictionary'
argPrecedence(' arg1', arg3='arg3', arg2='arg2', arg5='arg5')

# argPrecedence Results:
par1 = arg1 # positional argument
par2 = arg2 # keyword argument
par3 = arg3 # keyword argument
par4 = () # argument converted to tuple
par5 = {'arg5': 'arg5'} # argument converted to dictionary
## =============== =============== =============== ===============

The code above is verbose with comments because I am just learning
Python and am creating my own examples and reference materials... Can
you solve the problem? If so, can you share the solution? If not, is
this a bug, limitation or is it a feature?

Thank you for your time on this one!

PS. I could be far off with this codes purpose; to try and create an
example that will house all the different possible parameters
(positional, keyword, * and **) in one single def statement.

--
Best Regards
Victor B. Gonzalez

Mar 5 '06 #1
11 2025
Please forgive my call()

Change this:
argPrecedence(' arg1', arg3='arg3', arg2='arg2', arg5='arg5')

to this:
argPrecedence(' arg1', par3='arg3', par2='arg2', arg5='arg5')

Thanks!

Mar 5 '06 #2
Hello all,

I am just learning Python and have come across something I feel might
be a bug. Please enlightenment me... The following code presents a
challenge. How in the world do you provide an argument for *arg4?

## =============== =============== =============== ===============
def argPrecedence(p ar1, par2=0, par3=0, *par4, **par5):
print 'par1 =', par1, ' # positional argument'
print 'par2 =', par2, ' # keyword argument'
print 'par3 =', par3, ' # keyword argument'
print 'par4 =', par4, ' # argument converted to tuple'
print 'par5 =', par5, ' # argument converted to dictionary'

argPrecedence(' arg1', par3='arg3', par2='arg2', arg5='arg5')

# argPrecedence Results:
par1 = arg1 # positional argument
par2 = arg2 # keyword argument
par3 = arg3 # keyword argument
par4 = () # argument converted to tuple
par5 = {'arg5': 'arg5'} # argument converted to dictionary
## =============== =============== =============== ===============

The code above is verbose with comments because I am just learning
Python and am creating my own examples and reference materials... Can
you solve the problem? If so, can you share the solution? If not, is
this a bug, limitation or is it a feature?

Thank you for your time on this one!

PS. I could be far off with this codes purpose; to try and create an
example that will house all the different possible parameters
(positional, keyword, * and **) in one single def statement.

--
Best Regards
Victor B. Gonzalez

Mar 5 '06 #3
"vbgunz" wrote:
I am just learning Python and have come across something I feel might
be a bug. Please enlightenment me... The following code presents a
challenge. How in the world do you provide an argument for *arg4?

## =============== =============== =============== ===============
def argPrecedence(p ar1, par2=0, par3=0, *par4, **par5):
print 'par1 =', par1, ' # positional argument'
print 'par2 =', par2, ' # keyword argument'
print 'par3 =', par3, ' # keyword argument'
print 'par4 =', par4, ' # argument converted to tuple'
print 'par5 =', par5, ' # argument converted to dictionary'

argPrecedence(' arg1', arg3='arg3', arg2='arg2', arg5='arg5')

# argPrecedence Results:
par1 = arg1 # positional argument
par2 = arg2 # keyword argument
par3 = arg3 # keyword argument
par4 = () # argument converted to tuple
par5 = {'arg5': 'arg5'} # argument converted to dictionary
## =============== =============== =============== ===============

The code above is verbose with comments because I am just learning
Python and am creating my own examples and reference materials... Can
you solve the problem? If so, can you share the solution? If not, is
this a bug, limitation or is it a feature?


not sure what you think the bug is, but I suspect that you've completely
missed the point of what * and ** do, and how they're used in real code.

</F>

Mar 5 '06 #4
vbgunz wrote:
I am just learning Python and have come across something I feel might
be a bug. Please enlightenment me... The following code presents a
challenge. How in the world do you provide an argument for *arg4?


Maybe by putting that in the list of arguments? ...

You don't even *have* an arg4 in the list, so how are we supposed to
know what you are asking without guessing? Please consider rephrasing
the question.

-Peter

Mar 5 '06 #5
On Sun, 05 Mar 2006 04:45:05 -0800, vbgunz wrote:
Hello all,

I am just learning Python and have come across something I feel might
be a bug. Please enlightenment me... The following code presents a
challenge. How in the world do you provide an argument for *arg4?

## =============== =============== =============== ===============
def argPrecedence(p ar1, par2=0, par3=0, *par4, **par5):
print 'par1 =', par1, ' # positional argument'
print 'par2 =', par2, ' # keyword argument'
print 'par3 =', par3, ' # keyword argument'
print 'par4 =', par4, ' # argument converted to tuple'
print 'par5 =', par5, ' # argument converted to dictionary'


No, you are confused about * and ** arguments. They don't convert
arguments, they collect them.

def f(a, b=0, *args, **kwargs):
print "a =", a
print "b =", b
print "args =", args
print "kwargs =", kwargs

Now call the function different ways, and see which will work and which
won't.

f(1)
f(1, 2)
f(1, b=2)
f(a=1, b=2)
f(b=2, a=1)

Notice that this does not work:

f(b=3)

Do you understand why not?

Now look at these calls:

f(1, 2, 3)
f(1, 2, 3, 4, 5, 6)
f(1, 2, 3, 4, 5, 6, foo=7, bar=8)

In the function definition f(a, b=0, *args, **kwargs), a must be supplied,
b has a default value so it can be left out. Both a and b can be provided
as positional arguments like f(2, 3) or as keyword arguments.

Any _extra_ positional arguments are collected into a tuple called args,
and any _extra_ keyword arguments are collected into a dictionary called
kwargs. Of course you can use any names you like.

Hope this helps,

--
Steven.

Mar 5 '06 #6
On Sun, 05 Mar 2006 04:45:05 -0800, vbgunz wrote:
PS. I could be far off with this codes purpose; to try and create an
example that will house all the different possible parameters
(positional, keyword, * and **) in one single def statement.


This is usually a bad idea, not because Python can't cope with it, but
because it is usually better to learn new things in small pieces, not one
giant enormous piece.

Try creating a number of small functions that do different things:

def f(a, b, c):
print "a =", a, "b =", b, "c =", c

def g(a=0, b=0, c=0):
print "a =", a, "b =", b, "c =", c

Now call them different ways, and see what happens:

f()
f(1)
f(1,2)
f(1,2,3)
f(b=2)

Can you see a pattern?

g()
g(1)
g(1,2)
g(1,2,3)
g(b=2)

Then move on to argument collectors:

def h(a, b, c=0, *d, **e):
print "a =", a, "b =", b, "c =", c
print "d =", d, "e =", e

Also, remember that "positional arguments" and "keyword arguments" aren't
defined differently, they are given when you call the function. For
example, suppose you have this:

def function(x, y):
print x + y

Now you call it with positional arguments: function(3, 7)
Now you call it with keyword arguments: function(x=3, y=7)
Now you call it with both: function(3, y=7)

All from the same definition. An argument is positional or keyword
according to how it is given, not how it is defined.
--
Steven.

Mar 5 '06 #7
I am sorry I hung you up on a typo Peter Hansen. On line 5 *arg4 should
have been *par4. I hope it makes complete sense now. Sorry.

Mar 5 '06 #8
Please allow me some time to look at your examples. I get hung up over
the smallest details because in my mind, my approach should have just
worked... I learned about these parameters reading "O'reilly Learning
Python 2nd Edition". On page 217 of the paperback or Chapter 13.5.6 in
the ebook, topic: 'Argument matching: The Gritty Details' mentions the
following in verbatim...

....'''
Moreover, Python internally carries out the following steps to match
arguments before assignment:
1. Assign non-keyword arguments by position.
2. Assign keyword arguments by matching names.
3. Assign extra non-keyword arguments to *name tuple.
4. Assign extra keyword arguments to **name dictionary.
5. Assign default values to unassigned arguments in header.
'''...

As you can probably tell, I tried to follow the steps exactly, except
step 5 got me lost so I tried not to question it. Anyhow, thank you
very much for your time and examples. I will get back to you as soon as
I am done putting your response to trial. Thank you!

Mar 5 '06 #9

"vbgunz" <vb****@gmail.c om> wrote in message
news:11******** *************@i 40g2000cwc.goog legroups.com...
Hello all,

I am just learning Python and have come across something I feel might
be a bug.
I feel it is more likely that there in a bug in the communication process
from the manual to you, on an admittedly somewhat confusing and complex
subject.
Please enlightenment me...
OK. Here is my attempt at a summary.

The parameters for a function are the local variable names listed in the
definition header in ()s after the function name. There may be any number
of regular parameters and up to two collection parameters.

The arguments for a function call are the objects listed in ()s after the
function object (usually but not necessarily indicated by name).
Argument objects are the result of evaluating an expression.
An argument is optionally named by prefixing the expression with 'name=',
with the restriction that all args after a named arg must also be named.

Regular paramenter names (and slots of collection parameters) are bound to
argument objects by position or name according to the rules given in the
manual. At the end of the process, all parameters and arguments must
exactly one binding. (For parameters, the binding may be a default given
in the definition.) If not, an exception is raised and the call aborted.
How in the world do you provide an argument for *arg4?
You obviously mean par4. See below for how it gets a non-empty value.
def argPrecedence(p ar1, par2=0, par3=0, *par4, **par5):
Par1, par2, and par3 are regular parameters. They can by matched either by
position or name on a per-call basis. Mixing position and name matching in
any one call is strongly discouraged as confusing and error prone.

Par2 and par3 have default arg objects (calculated when the function is
defined). That only means that they do not have to match any of the args
in a particular call. It does not determine whether the binding to
explicit args in a particular call will be by position or name.
print 'par1 =', par1, ' # positional argument'
print 'par2 =', par2, ' # keyword argument'
print 'par3 =', par3, ' # keyword argument'
So these comments are wrong except as they happen to be right for a
particular call.
Par4 and par5 are leftover-argument collection objects bound by default to
() and {} respectively if there are, respectively, no leftover positional
or named args. So the way to get a nonempty tuple bound to par4 is to have
a leftover positional arg which requires that you give at least 4 (since
the first three will be bound to the three regular parameters).
print 'par4 =', par4, ' # argument converted to tuple'
print 'par5 =', par5, ' # argument converted to dictionary'
Wrong. Leftover args are placed in, not individually converted to, a tuple
or dictionary.
PS. I could be far off with this codes purpose; to try and create an
example that will house all the different possible parameters
(positional, keyword, * and **) in one single def statement.


Again, regular parameters are both positional and named, and the matching
on any particular call can be either (subject to the positional, then named
args restriction). In any case, what I think you mean above is not
possible. As stated previously, the *leftover_pos_a rg tuple will be
nonempty only if all regular params are positionally matched.

Terry Jan Reedy

Mar 5 '06 #10

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

Similar topics

20
8155
by: Vivek N | last post by:
Hi Folks, This question may well have been asked and answered before. But, sorry that I couldn't find one from the archives. I typed up this program and compiled it with gcc 3.3.2 main() { int i = -3,j= 2,k=0,m; m = ++i || ++j && ++k; printf("\n%d %d %d %d\n",i,j,k,m);
25
1726
by: noe | last post by:
Hello, I'm writing a file system filter driver and I've found in an example this sentence: if (VALID_FAST_IO_DISPATCH_HANDLER( fastIoDispatch, FastIoRead )) { return (fastIoDispatch->FastIoRead)( FileObject, FileOffset, Length, Wait,
21
3046
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as opposed to what is mentioned in precedence table? Also, with comma operator. consider,
11
1560
by: Rupesh | last post by:
Hello all, See the code .... int i=-3,j=2,k=0,m; m=++i;&&++j||++k; printf ("%d %d %d %d",i,j,k,m); I executed this code on gcc. the o/p i had got is:- -2 3 0 1
0
322
by: vbgunz | last post by:
Hello all, I am just learning Python and have come across something I feel might be a bug. Please enlightenment me... The following code presents a challenge. How in the world do you provide an argument for *arg4? ## ============================================================ def argPrecedence(par1, par2=0, par3=0, *par4, **par5): print 'par1 =', par1, ' # positional argument' print 'par2 =', par2, ' # keyword argument'
5
2542
by: junky_fellow | last post by:
Hi, I have a very basic doubt about associativity and precedence of operators. I am not a computer science person and may find it quite weird. Consider an expression 4+5*2
9
3749
by: marko | last post by:
/* code start */ int a = 0; /* expected evaluation and excution order with precedence in mind /* False(3) , True(1), False(2) */ if ( (a=1) == 0 || 0 != 1 && (a =2) == 1) putchar('T'); printf("%d", a); /* code end */ 2
32
3326
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x += i + j + k++;
5
3643
by: Peng Yu | last post by:
Hi, Please see the following code and the output. It seems that if one of %'s oprand is unsigned, the other will also be converted to unsigned. There is a operator precedence table in wikipedia. I'm wondering what is the general rule in type conversions. Thanks, Peng
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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
10215
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
9865
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
8872
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...
1
7410
isladogs
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...
0
6674
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();...
1
3964
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
3
2815
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.