Re: What is the "Unpacking Arguments List" rule?

2018-06-13 Thread sa...@caprilion.com.tw

[Of the first part]
line 19 is
action(progress=progress, *args)
where the args is a tuple
args = (i, 3)
and the function is defined as
def action(id, reps, progress):

In documents 4.7.2. Keyword Arguments, it says
'''
def parrot(voltage, state='a stiff', action='voom', type='Norwegian 
Blue'):

...
...
but all the following calls would be invalid:
...
parrot(voltage=5.0, 'dead')  # non-keyword argument after a keyword 
argument

...
'''

After unpack the args in line 19, it will be looks like
action(progress=progress, i, 3)
and it seems violate the above rule.

[Of the second part]
> The "*args" and "**kw" are very helpfull. I hope (and expect)
> they will remain.
There is no "**kw" involved in this topic:-) and the positional limit of
"*args" in a function definitions has its own good reason:-)

IMHO, there is no reason to check the *args has to appear at last in
positional argument list in a function call because of there is no
"unknown number of parameters" at the time of unpacking. It should be
alright to write line 19
action(*args, progress)
just like assignment below where both are valid.
a, *b = any
*a, b = any

Best Regards,
Jach Fong


dieter at 2018/6/13 PM 12:59 wrote:

Jach Fong  writes:

...
4.7.4. Unpacking Argument Lists
The reverse situation occurs when the arguments are already in a list or
tuple but need to be unpacked for a function call requiring separate
positional arguments.
 ...

args = [3, 6]
list(range(*args))

"""

I can't understand why line 19 works?


Not sure what "line 19" is - but if it refers to the example above:

   "range" accepts (among others) 2 integer arguments.
   The "*args" above means: unpack the sequence in "args" into
   individual arguments.
   This means (with the values of the example above),
   that "range(*args)" is equivalent to "range(3, 6)".


Didn't it violate the rule of
"# non-keyword argument after a keyword argument"?


No keyword arguments at all in the above example.


and why a more
reasonable look syntax gets an error?

 action(*args, progress)
  ^
SyntaxError: only named arguments may follow *expression  File
"test.py", line 19


This is (in my view) a somewhat arbitrary restriction -- maybe
introduced for symmetry with the function definition syntax.

The message is quite clear, however: after the "*arg",
you must pass keyword arguments only, i.e. they must have
the form "param=value".


The only reason I can guess is that it checks with the rule in 4.7.3
which is really unrelated. The "*any" notation used in different places
with different meaning, such as defining arbitrary argument, unpacking
argument or even in an assignment(a,*b=any). Maybe it will be better to
stop this syntax checking and lets both statements below valid:-)


The "*args" and "**kw" are very helpfull. I hope (and expect)
they will remain.



---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

--
https://mail.python.org/mailman/listinfo/python-list


Re: Why an object changes its "address" between adjacent calls?

2018-06-17 Thread sa...@caprilion.com.tw

Jim Lee at 2018/6/17 PM 04:10 wrote:



On 06/17/2018 12:08 AM, Jach Fong wrote:

C:\Python34\Doc>py
Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 
32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter as tk
>>> root = tk.Tk()
>>> tk.Label(root, text='label one', font='TkDefaultFont').pack()
>>> from tkinter import font
>>> font.nametofont('TkDefaultFont')

>>> font.nametofont('TkDefaultFont')

>>>

The "address" of the Font object 'TkDefaultFont' changes, why?

Best Regards,
Jach Fong




def nametofont(name):
     """Given the name of a tk named font, returns a Font representation.
     """
     return Font(name=name, exists=True)

Every time you call nametofont(), you're creating a new instance of the 
Font class.


hmm... It means every time I set a widget's font to 
"TkDefaultFont", a new object was created. Why python do things this 
way? Can't it use

this same object again and again?

--Jach

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

--
https://mail.python.org/mailman/listinfo/python-list


Re: Why an object changes its "address" between adjacent calls?

2018-06-18 Thread sa...@caprilion.com.tw

Grant Edwards at 2018/6/18 PM 10:36 wrote:

On 2018-06-17, Jach Fong  wrote:

C:\Python34\Doc>py
Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32
bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

import tkinter as tk
root = tk.Tk()
tk.Label(root, text='label one', font='TkDefaultFont').pack()
from tkinter import font
font.nametofont('TkDefaultFont')



font.nametofont('TkDefaultFont')






The "address" of the Font object 'TkDefaultFont' changes, why?


What makes you think it's the same object the second time and not a
new object?


Simply from what the method's name "name-to-font" implied. The font is 
already there, so naturally it should be the same one:-)



---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

--
https://mail.python.org/mailman/listinfo/python-list