Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103843

Re: How to know if an object is still be referenced?

From Terry Reedy <tjreedy@udel.edu>
Newsgroups comp.lang.python
Subject Re: How to know if an object is still be referenced?
Date 2016-03-02 02:03 -0500
Message-ID <mailman.95.1456902233.20602.python-list@python.org> (permalink)
References <8e9f1a84-cced-435e-a379-e1e2ac03f483@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to know if an object is still be referenced? jfong@ms4.hinet.net - 2016-03-01 18:35 -0800
  Re: How to know if an object is still be referenced? Terry Reedy <tjreedy@udel.edu> - 2016-03-02 02:03 -0500
    Re: How to know if an object is still be referenced? jfong@ms4.hinet.net - 2016-03-02 03:35 -0800
      Re: How to know if an object is still be referenced? sohcahtoa82@gmail.com - 2016-03-02 15:38 -0800
        Re: How to know if an object is still be referenced? jfong@ms4.hinet.net - 2016-03-02 18:36 -0800

csiph-web