1

Is it possible to create an object where object name is specified by a string

E.g.

create_object(QtGui.QLineEdit, 'myname')
myname.setText = 'created!'

That is

create_object(QtGui.QLineEdit, 'myname')

equals to

myname = QtGui.QLineEdit(self)
Pygmalion
  • 785
  • 2
  • 8
  • 24

3 Answers3

1

You can use globals. Example:

class A:
    a = 'Hello'

test = globals()['A']
print test.a #It will print 'Hello'

For more information: http://www.diveintopython.net/html_processing/locals_and_globals.html

Raphael Amoedo
  • 4,233
  • 4
  • 28
  • 37
1

To create local variable, you can get the dictionary of global variables using globals() function . Example -

gbl = globals()
gbl['myname'] = QtGui.QLineEdit
gbl['test'] = 1234
test
>> 1234

For local variables with locals() function , which returns the dictionary of local variables (a copy of the local namespace) , you may use this to set the variable, only if you are outside a function and directly in the script part, but setting to the dictionary provided by locals() would not work inside a function (you will not be able to access that variable , even in that function) , when using it outisde the function it has exactly same effect as globals().

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • You cannot use `locals` to define local variables. – Daniel Jun 21 '15 at 17:22
  • Yes, you can, did you try it? – Anand S Kumar Jun 21 '15 at 17:23
  • You probably didn't try it. – Daniel Jun 21 '15 at 17:25
  • What do you mean, my above pasted code works (If you take out the Qt stuff, which is something specific to OP's question). Please, note local() means local to the script, not local to function – Anand S Kumar Jun 21 '15 at 17:25
  • only because `locals() is globals()`. – Daniel Jun 21 '15 at 17:26
  • No they are not, check [here](http://stackoverflow.com/questions/7969949/whats-the-difference-between-globals-locals-and-vars) – Anand S Kumar Jun 21 '15 at 17:28
  • You are using `locals` on the top level, and this is not a local namespace but the global one: `>>> locals() is globals() -> True` – Daniel Jun 21 '15 at 17:42
  • I have just given an example, he can use it anywhere he wants, what I meant by locals is that if the variable is defined using locals its in the local namespace, not global namespace (meaning won't be accessible in other scripts (where this may be imported from) etc) . – Anand S Kumar Jun 21 '15 at 17:46
  • we do not know where they want to use it, locals() only sets variables in the local namespace (like I stated) , whereas globals sets them in the globals namespace. Its not like I provided complete code for him to just copy, I just gave simple example (even stated that this is an example of how to use it) – Anand S Kumar Jun 21 '15 at 17:48
  • Your example only works, because you are using `locals` in a global namespace, put your example in a function, and you will get a NameError. – Daniel Jun 21 '15 at 17:48
  • Yes, exactly that is why I said `locals()` creates local variables , what are you even trying to say? – Anand S Kumar Jun 21 '15 at 17:50
  • I want to say: You cannot create local variables! – Daniel Jun 21 '15 at 17:52
  • What do you mean by that? Two comments above you said when you use `locals()` inside a function and create a variable, you cannot use it outside the function (Please note you can still use it inside the function) and that is because `locals() `creates variables that are local to that function (function namespace) , when you use it in a single script (outside any function) you create variables that are local to that script. So yes you can create variables, or please explain what you mean by `you cannot create local variables!` – Anand S Kumar Jun 21 '15 at 17:54
  • Thanks, I fixed the answer. – Anand S Kumar Jun 21 '15 at 18:16
0

You can create an attribute with a string specifying the object name with setattr(), like this:

setattr(sys.modules[__name__], 'myname', QtGui.QLineEdit)
khagler
  • 3,996
  • 29
  • 40