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

Terry Reedy tjreedy at udel.edu
Tue Jun 19 08:35:08 EDT 2018


On 6/18/2018 8:38 PM, sales at caprilion.com.tw wrote:
> Grant Edwards at 2018/6/18 PM 10:36 wrote:
>> On 2018-06-17, Jach Fong <jfong at ms4.hinet.net> 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')
>>> <tkinter.font.Font object at 0x021E9490>
>>>>>> font.nametofont('TkDefaultFont')
>>> <tkinter.font.Font object at 0x021E9390>
>>>>>>
>>>
>>> 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:-)

'nametofont' is a trivial function returning 'Font(name=name, 
exists=True)'.  As explained before, the address is the address of the 
Python Font interface object, not the tk font structure.  tk has a 
mapping of font names to font structures.  tkinter does not keep a dict 
mapping names or font structures to Font instances, so each call to Font 
returns a new Font instance.

-- 
Terry Jan Reedy




More information about the Python-list mailing list