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

Chris Angelico rosuav at gmail.com
Sun Jun 17 06:28:43 EDT 2018


On Sun, Jun 17, 2018 at 7:39 PM, sales at caprilion.com.tw
<sales at caprilion.com.tw> wrote:
> 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')
>>> <tkinter.font.Font object at 0x021E9490>
>>> >>> font.nametofont('TkDefaultFont')
>>> <tkinter.font.Font object at 0x021E9390>
>>> >>>
>>>
>>> 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?
>

That would imply keeping the object around until it's needed again.
Sometimes that's worth doing; other times it isn't, or isn't worth the
hassle. As soon as you stop using the previous one, Python is free to
dispose of it.

ChrisA



More information about the Python-list mailing list