1

I am working on a python GTK project and was wondering if it is possible to generate random variable names for naming widget objects and returning them so that it could be used in other parts of program like :

#Class AppGUI:

def createButton(self, buttonlabel):
    RandombuttonObjectName = gtk.Button(self.buttonlabel)
    RandombuttonObjectName.show()
    return RandombuttonObjectName


#Main Program

Button1 = AppGUI.createButton("Button 1")
Button2 = AppGUI.createButton("Button 2")

Kindly suggest how to achieve this random object naming and returning from class to main program.

avimehenwal
  • 1,502
  • 3
  • 21
  • 30
  • 4
    ... Are you aware that the variable name has **nothing** to do with the object? Provide a use-case where generating random variable names is useful and maybe someone could answer you. – Bakuriu Apr 29 '13 at 12:19
  • I think a name is always required to refer any object/instance of any class. – avimehenwal Apr 29 '13 at 14:23
  • 1
    No, that's completely false: `a = ["some string"]`. Now I can refer to the string `"some string"` via `a[0]` and note that `a` is *not* a reference to the string. Also, you could use `locals("a")[0]` which doesn't contain any name except the name of a built-in. In conclusion: we don't need names to refer to objects, we can do many things without them. Anyway, what I meant, is that the name of the variable isn't strictly related to the object it references. You can assign it to anything: `a = 5; a = 7`, or even delete it `del a`. – Bakuriu Apr 29 '13 at 14:51
  • Specific to my user case @Bakuriu I am trying to find out way such that python automatically creates object references for me and which I can use in later part of program. Means, In my code I am creating new buttons from user class method createButton() want to reference it by name Button1 so that I could use this future part of program to alter its properties/values. – avimehenwal Apr 30 '13 at 08:16

1 Answers1

4

As you've written it, createButton already returns unique, essentially random references to widgets.

(To be more precise, it returns the widgets themselves, but I find it clearer to think of them as references.)

msw
  • 42,753
  • 9
  • 87
  • 112