-3

I have hundreds of variables (here is only few of them so you'll get idea how this code works) and I have no idea how to solve this problem. What I want to do is in second if statement to somehow modify variable before declaring it so I could copy class to it so I could bypass many needless if satements. Is it possible?

This code in second if statement of course is incorrect

globals()["p%sg%sr01_id" % (cp, awsg)]) = copy.copy(temp1)

but this is what I'd like to achieve before declaring variable to which I'll copy class

import copy
cp = "01"
awsg = "01"
temp1 = 0
temp2 = 0

first_hovered_variable = "01"
second_hovered_variable = "02"


class Char():
    def __init__(self, name):
        self.n = name

p01g01r01_id = Char("A1")
p01g01r02_id = Char("B2")
p01g01r03_id = Char("C3")

reg01_id = Char("001")
reg02_id = Char("002")
reg03_id = Char("003")


def swap_variables():
    global cp
    global awsg

    global temp1
    global temp2

    global first_hovered_variable
    global second_hovered_variable

    global reg01_id
    global reg02_id
    global reg03_id

    global p01g01r01_id
    global p01g01r02_id
    global p01g01r03_id

    if first_hovered_variable == second_hovered_variable:
        first_hovered_variable = 0
        second_hovered_variable = 0

    elif first_hovered_variable != 0 and second_hovered_variable != 0:
        temp1 = copy.copy(globals()["p%sg%sr%s_id" % (cp, awsg, second_hovered_variable)])
        temp2 = copy.copy(globals()["p%sg%sr%s_id" % (cp, awsg, first_hovered_variable)])

        if first_hovered_variable == "01":
            reg01_id = copy.copy(temp1)
            globals()["p%sg%sr01_id" % (cp, awsg)]) = copy.copy(temp1)

        elif first_hovered_variable == "02":
            reg02_id = copy.copy(temp1)
            globals()["p%sg%sr02_id" % (cp, awsg)]) = copy.copy(temp1)

        elif first_hovered_variable == "03":
            reg03_id = copy.copy(temp1)
            globals()["p%sg%sr03_id" % (cp, awsg)]) = copy.copy(temp1)    

        if second_hovered_variable == "01":
            reg01_id = copy.copy(temp2)
            globals()["p%sg%sr01_id" % (cp, awsg)]) = copy.copy(temp1)    

        elif second_hovered_variable == "02":
            reg02_id = copy.copy(temp2)
            globals()["p%sg%sr02_id" % (cp, awsg)]) = copy.copy(temp1)

        elif second_hovered_variable == "03":
            reg03_id = copy.copy(temp2)
            globals()["p%sg%sr03_id" % (cp, awsg)]) = copy.copy(temp1)

    temp1 = 0
    temp2 = 0

swap_variables()
print p01g01r01_id.n
print p01g01r02_id.n
print p01g01r03_id.n

print reg01_id.n
print reg02_id.n
print reg03_id.n
Gunnm
  • 974
  • 3
  • 10
  • 21
  • Possible duplicate of [Dynamic variable name in python](http://stackoverflow.com/questions/2564140/dynamic-variable-name-in-python) – Morgan Thrapp Oct 01 '15 at 16:31
  • 8
    Your code is, to be perfectly frank, horrible. You have huge amounts of repetition (immediate sanction from the Redundancy Department of Redundancy), loads of `global` variables, a pointless class, huge amounts of repetition, ... Once you do get this working, consider visiting [codereview.se] for some feedback. – jonrsharpe Oct 01 '15 at 16:31
  • 1
    Try to go *with* the language rather than *against* it. – Peter Wood Oct 01 '15 at 16:31
  • There is no such thing as "variable declaration" or "variable initialization" in Python. – Ali Gajani Oct 01 '15 at 16:31
  • You've got a typo on those lines - get rid of the extra close paren so that they look like `globals()["..." %(cp, awsg)] = copy.copy(temp1)` – Rob Watts Oct 01 '15 at 16:32
  • 2
    Variables can't be modified, only what they point to. – Peter Wood Oct 01 '15 at 16:33
  • Classes are pointless because it wouldn't do any good to code transparency if I would make them much more complicated. Sometimes guys it would be nice if you could just try to focus on question not how code looks like. I could tell you that hovered variable changes whole time etc etc but it's not important. What is important for me is question asked. – Gunnm Oct 01 '15 at 16:38
  • 4
    That's why I commented; I know it's not an answer to your question. *"it wouldn't do any good to code transparency if I would make them much more complicated"* - but it would improve it if you got rid of them altogether. My point is that you can get help for your broader problems, too; as this is a site for professional and enthusiastic programmers (per the [tour]), I tend to assume that OPs have *some* interest in improving their skills. – jonrsharpe Oct 01 '15 at 16:42
  • I want to copy class and I want to do this with globals() I think it's important that you know that I want to do this. Variables are also simple because it's not important if it's "1" or "Harvey Jones" but that it is there and it is used. I'm new and I want to learn but instead of that you just trash what you see without telling me how I can improve. This is not helpful at all. – Gunnm Oct 01 '15 at 16:45
  • 4
    @Gunnm *"I think it's important that you know that I want to do this"* - and I think it's important that you know that there are other, better ways of approaching the problem. *"without telling me how I can improve"* - I *am* telling you how you can improve; **get your code reviewed!** Note also that you need to include `@username` to notify other users that you're addressing them. – jonrsharpe Oct 01 '15 at 16:46
  • 2
    If your code needs hundreds of `if` statements to work, *you are doing it wrong*. – rlbond Oct 01 '15 at 16:48

1 Answers1

4

Yes, you can set new globals directly in the globals() directory, without ever using any other assignments to those names.

However, you should consider creating a separate dictionary or list for your instances instead. That way you can address those instances as a group.

For your 3 dimensions, you could even produce a nested structure:

characters = [
    [
        [Char(letter + digit) for letter, digit in zip('ABC', '123')]
        for g in range(3)
    ] for p in range(3)
]

Do the same for your 3 reg variables; store those in a list too.

Now you can address characters[0][1][2] to get Char('C3') at that position. Store those indices in variables and you can use those to address different positions:

reg[first_hovered_variable] = characters[cp][awsg][first_hovered_variable]

and you removed a whole set of if statements.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343