Problem with assign,

Asked

Viewed 38 times

0

class transaction:
...
def send(from_key, to_key, SN):
    if SN in from_key.parts:
        str(from_key)+str(to_key)+str(SN)=transaction(SN,from_key,to_key,True)

Syntaxerror: can’t assign to Operator

I can’t assign a int to the "transaction" class... Let’s assume that

from_key = a
 to_key = b
 SN = 123

I want to arrive at the following result: ab123 = transaction(SN,from_key,to_key,True)

Manually (out of function), it works, but when I try to automate in the function I get this error.

Update:

I need to give a name to a class (transaction) and I’ve determined that name will be mine from_key + to_key + SN, in my case would be ab123. The problem is that every time I go to perform a transaction that name will change, and I created a function to perform this assing alone (ab123 = transaction(...)). How can I make this work?

  • You can’t make a dynamic assignment this way. What you want is to dynamically generate the variable "ab123" according to the function parameters? And what will you do with this variable?

  • I want to generate the variable according to what I entered in the send function, in my example would be send(a,b,123). My problem is when I try to assign the variable to a class. Why ab123 = transaction(...) works and str(from_key)+str(to_key)+str(SN)=transaction(...) nay?

  • Why do you need to do this? What will you do with the variable?

  • It is not possible to assign a value to another value. In the case you are doing something like "a" + "b" +"123" = transaction(...). What you can do and create a dictionary and insert these values using the generated string as key.

  • how would I do it @fernandosavio?

  • I will formulate an answer to facilitate

  • The variable must become a transaction, which in turn stores information from the same @Andersoncarloswoss ...

  • And why can’t it be a normal variable, like trans = transaction(...)?

  • Because I would have to do it manually... I want the function, according to what I entered into it, to do the automatic assing. This function will perform other actions along with this...@Andersoncarloswoss

  • 2

    That sounds like a XY problem. I advise you to consider the idea of [Edit] the question and explain by text what you are trying to do in the general context. It makes no sense to define a dynamic variable like this.

  • I updated the post trying to explain better @Andersoncarloswoss

Show 6 more comments

1 answer

0

You can create a dictionary and use the generated string as a key:

cache = dict()

def send(from_key, to_key, SN):
    if SN in from_key.parts:
        key = str(from_key) + str(to_key) + str(SN)
        cache[key] = transaction(SN, from_key, to_key, True)

To recover the result of transaction(...) just use:

print(cache['ab123'])  # ou qualquer que seja a chave
  • Returns: NameError: name 'cache' is not defined

  • It needs to be cached in the scope of where you are trying to access it... It will depend on how you coded.

  • I am programming in the cloud, in a notebook jupyter.

  • I refer to the scope of python itself. looks that example as long as you can access the cache variable you can save and read information

  • thus it is in the link it does not define the variable ab123. I would like to be able to define the variable ab123 as being a transaction class.

  • The result would have to be the same as ab123 = transaction(...)

  • John, it doesn’t make sense for you to define variables dynamically. How will you access the variables later if you don’t know their names? What you are definitely doing is an XY problem and any solution will be gambiarra. If you want a name, pass by parameter a string. If you want to persist this object somewhere, it persists by associating the name to the object. Without knowing the whole problem, any answer here will be mere speculation, with 96.37% chance of being gambiarra.

  • A dict() It is precisely for this. In the example I sent you could be an instance of transaction, no problem. The difference is that instead of accessing as ab123 accesses cache['ab123']

  • The problem of creating "dynamic variables" is solved with dict, but it’s like @Andersoncarloswoss commented. What we know about your problem is very shallow, so we can only hope that’s what you need.

  • Sorry about that, as I said, I have no knowledge in the area. If you want to look at the code here: https://github.com/veronamlima/notebook/blob/master/Blockchain%20V3%20(class). ipynb

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.