Is it possible to call a class but without a new instance created?

Terry Reedy tjreedy at udel.edu
Mon Jun 18 05:09:21 EDT 2018


On 6/18/2018 12:48 AM, Jach Fong wrote:
> After looking into the \tkiniter\font.py source file, triggered by Jim's
> hint on my previous subject "Why an object changes its "address" between
> adjacent calls?", I get more confused.
> 
> Below was quoted from the font.py:
> ------------------------
> def nametofont(name):
>      """Given the name of a tk named font, returns a Font representation.
>      """
>      return Font(name=name, exists=True)
> 
> class Font:
>      """Represents a named font.

tkinter abbreviates tk interface.  A Python tkinter Font instance 
represents a tk named font structure. It has a hidden pointer to the tk 
structure.  The same is true of all instances of tkinter widgets 
classes.  Each has a hidden pointer to a tk widget

>      Constructor options are:
>      ...
>      exists -- does a named font by this name already exist?

Does a *tk* named font exist?

>         Creates a new named font if False, points to the existing font 
> if True.

Again, 'font' here means a tk structure, not a python instance.  Each 
call to Font returns a new python instance.  But for Fonts, it may or 
may not point to a new tk structure.

>      ...
>      """
> 
>      def __init__(self, root=None, font=None, name=None, exists=False,
>                   **options):
>          ...

One can mostly ignore the parallel existence of python instances and tk 
structures.  But they can get out of sync during shutdown.  If t is an 
instance of Text, t.destroy() causes tkinter to tell tk to destroy the 
tk widget, leaving t useless.  Similarly, if 'del t' deletes the last 
reference to the Python instance, it may disappear, leaving the tk 
widget possibly unaccessible.

-- 
Terry Jan Reedy





More information about the Python-list mailing list