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

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


To answer the question of the title, which is a bit different from the 
question in the text, yes.  type(None)() always returns the singleton 
None object.  (And one can write a singleton class in Python also.) 
bool() always returns one of False or True.  int() and str() may return 
either a new or old object.  For such immutables, it does not matter as 
long at the object has the correct value.  As others said, this is all 
handled in a __new__ method.  But none of this has much to do with 
tkinter instances.

On 6/18/2018 5:09 AM, Terry Reedy wrote:
> 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