1

Is there a declaration/assign function in MATLAB or Python which works in both ways, bilateral/mirror type of use? Example;

x = 3
print x
#output is 3#

but what if there is a type of function, like this;

x _=_ 3
print x
# output is 3 #
print 3
# output is x #
Cœur
  • 37,241
  • 25
  • 195
  • 267
Lindy
  • 11
  • 5
  • 1
    Most languages don't support that sort of thing. You may find this article helpful: [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html), which was written by SO veteran Ned Batchelder. – PM 2Ring Jul 30 '16 at 15:42
  • OTOH, there _is_ a way in Python to find all names in the current local or global scope that are bound to a particular object, but it's _not_ the sort of thing you do in a normal program. :) – PM 2Ring Jul 30 '16 at 15:45

4 Answers4

3

In Python, no. In Matlab, probably no. What your asking for doesn't even really make sense. How can you assign x, a name, to 3, an integer literal? You can't assign anything to an integer literal, anyway! And what do you mean by x? The string "x"? The functionality you are probably looking for would be achieved by a map. Essentially, a map is a collection of key, value pairs where you access the value with the key. Keys must be unique, but the same value can have different keys. Python has a built-in data structure called a dict for this very purpose:

num_to_letter = {1:'x', 2: 'y'}
print(num_to_letter[1])
print(num_to_letter[2])

You can choose whether to contain the reverse mapping in the same dict or make another dict for that purpose.

letter_to_num = {}
for key in num_to_letter:
    letter_to_num[num_to_letter[key]] = key

print(letter_to_num)

Once you get used to python, you can start using dictionary comprehensions in python 3:

letter_to_num = {v:k for k,v in num_to_letter.items()}

In python 2, you will have to use the following:

letter_to_num = dict((v,k) for k,v in num_to_letter.items()) 
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • 1
    Python 2.7 supports the dict comprehension syntax. – PM 2Ring Jul 30 '16 at 16:11
  • Yes, a string, a word, set of characters, but not a variable, in a way that at the same time `string = integer` and `integer = string`. So for example; when sorted by integer in a matrix, i could `print` their respective string in the same order as integer sorted. – Lindy Jul 30 '16 at 16:18
  • @Lindy The simple way to do that kind of thing is to make a list of (name, integer) tuples. You can sort such a list alphabetically or numerically. – PM 2Ring Jul 30 '16 at 16:22
2

That would be trying to set the value of a literal like it was a variable, which you can't do. The number 3 cannot be the name of a variable with the value of "x", nor can it be an alias to a variable as far as I know.

1

In MATLAB, no. You can use only lower and uppercase letters as the start of a variable name.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
0

BIG NO.

Check out python variable name rules in Google

You cannot assign anything to a number and make it a variable.

Another reason is making a number a variable is allowing to vary it's value. Ideally 3 should be always 3

Almost in all modern languages, a number cannot be used as a variable.

be_good_do_good
  • 4,311
  • 3
  • 28
  • 42