Roy Holzem
Roy Holzem

Reputation: 870

Defining a Variable without using the Equal sign

I try to understand something,

x = 10
print x

The result will be 10

x is 10
print x

The result will be Error X is not defined.

x = 10
y = 10

if x == y:
  print True

if x is y:
  print True

The result is:

True
True

Is there another way to define a variable without using the equal sign?

Upvotes: 2

Views: 2791

Answers (4)

zahypeti
zahypeti

Reputation: 183

When you're in the interactive Python interpreter the return value of the last expression is automatically stored in the _ (underscore) variable:

>>> 1.0 + 1.0
2.0
>>> _
2.0
>>> _ * 2
4.0
>>> _
4.0

Furthermore, if you're using IPython,

  • __ and ___ (double and triple underscores) refer to the second to last and third to last output values,
  • _iii, _ii, _i refer to the last three input commands,
  • any previous output can be accessed using something like _2, and
  • any previous input can be accessed as a string using something like _i2:

(See a corresponding Stackoverflow answer.)

In [1]: True
Out[1]: True

In [2]: 'asdbsd'
Out[2]: 'asdbsd'

In [3]: -5 + 0
Out[3]: -5

In [4]: ___, __, _
Out[4]: (True, 'asdbsd', -5)

In [5]: _i
Out[5]: u'___, __, _'

In [6]: _1
Out[6]: True

In [7]: _i3
Out[7]: u'-5+0'

(IPython documentation.)

Upvotes: 1

Vedarth Sharma
Vedarth Sharma

Reputation: 329

You cannot declare a variable using "is" as "is" compares the identities of operands (a stricter version of == imo) not assigns it. Back to your question, I don't think that's necessary as "=" serves that role quite well. However in a block of code, like for loop, you can assign temporary variables in that block of code. Example:-

for i in range(20):
    print i

or

You can also use "as" keyword like

import xml.etree.ElementTree as ET

and

with open('file.txt') as f:
    a = f.readlines()

It all depends on what you mean by 'define a variable'.

Upvotes: 1

inspectorG4dget
inspectorG4dget

Reputation: 114035

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

Upvotes: 6

bsivo
bsivo

Reputation: 36

The short answer is not really, but that doesn't mean empty declarations aren't ever used in python. If you want to declare a variable but not instantiate it, your best bet is

x = ''

or

x = Null

There are times when you'd want to do this, despite what others have mentioned. For example, consider this code.

try:
    myValue = foo()
except:
    pass

print(myValue);

If foo() throws an error, your code won't break because of the try-catch. However, when you try to print myValue, your code will break because it's not defined. To prevent this you could add an empty declaration of myValue above the try-catch

Upvotes: 0

Related Questions