Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Terry Reedy Newsgroups: comp.lang.python Subject: Re: How to know if an object is still be referenced? Date: Wed, 2 Mar 2016 02:03:33 -0500 Lines: 44 Message-ID: References: <8e9f1a84-cced-435e-a379-e1e2ac03f483@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de VDdfeoCkmsXt+C4RzQvixw/Nllbf3YSmFvkajMvfUZxw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'exit': 0.07; 'rewrite': 0.07; 'subject:How': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:still': 0.09; 'jan': 0.11; 'def': 0.13; 'explicitly': 0.15; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'redundancy': 0.16; 'reedy': 0.16; 'subject:object': 0.16; 'suggestion.': 0.16; 'tk()': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'correctly.': 0.22; 'tkinter': 0.22; 'second': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'then.': 0.27; 'function': 0.28; 'referenced': 0.29; 'work.': 0.30; 'another': 0.32; 'noticed': 0.32; 'class': 0.33; 'label': 0.35; 'question,': 0.35; 'problem.': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'display': 0.37; 'received:org': 0.37; 'delete': 0.38; 'skip:p 20': 0.38; 'to:addr:python.org': 0.40; 'received:96': 0.63; 'times': 0.63; 'binding': 0.66; 'here': 0.66; 'worth': 0.67; 'picture': 0.70; 'saving': 0.70; 'photo': 0.81; 'disappears': 0.84; 'received:fios.verizon.net': 0.91; 'subject:know': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: pool-96-227-207-81.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 In-Reply-To: <8e9f1a84-cced-435e-a379-e1e2ac03f483@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103843 On 3/1/2016 9:35 PM, jfong@ms4.hinet.net wrote: > Recently I was puzzled by a tkinter problem. The codes below (from a book) can display the picture correctly. > > gifdir = "../gifs/" > from tkinter import * > win = Tk() > photo = PhotoImage(file=gifdir + "ora-pp.gif") > Button(win, image=photo).pack() > win.mainloop() Since photo is a global name, the binding remain until you explicitly delete it or exit the app. > And the codes below (from another book) will also work. > > class DrumMachine: > .... > .... > def create_play_bar(self): > .... > .... > photo = PhotoImage(file='images/signature.gif') > label = Label(playbar_frame, image=photo) > label.image = photo > label.grid(row=start_row, column=50, padx=1, sticky='w') > .... > .... Here photo is a local name and the binding disappears when the function exits. I would rewrite it to follow pattern 1. self.photo = PhotoImage(file='images/signature.gif') label = Label(playbar_frame, image=self.photo) To me, saving an attribute reference is not worth the extra line. > In the second example, I noticed that the "photo" was referenced two times > and I think it might be a redundancy so I remove the line "label.image = photo". But it fails then. On another question, I made the same suggestion. Oops. -- Terry Jan Reedy