Your question is pretty weird. In practice, you'd never want to not use =
to assign to a variable. But just for the sake of completeness, it is possible to assign to a new variable by screwing around with globals()
(or locals()
, depending).
Here's one way to do that:
globals().__setitem__('x', 10)
Proof:
In [139]: x
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-139-401b30e3b8b5> in <module>()
----> 1 x
NameError: name 'x' is not defined
In [140]: globals().__setitem__('x', 10)
In [141]: x
Out[141]: 10
EDIT:
Don't mess with locals()
. That's the mentally unstable, overly buff, drunk guy at the bar that'll f--- up your codez and make you say "he be cray cray".
So just don't mess with locals()
. On the other hand, just use x = 10
and save your sanity. Bah! this post physically hurts