How to unpack contents of list in a class method in python? -


I have a python class, and this is one of the methods I want to define the properties inside this method and I I want to assign items to a list, but this will not allow me to:

  class reader thread (threading thread): def __init__ (self, telex_path, ac_type): threading. Thread .__ init __ (self) self Message = [1,2,3] DEF calculation (self): self. Message_type, self.new_date, self.old_code = self.message  

This says:

  attribute error: 'ReaderTrade' object has no attribute Traceback:  Thread-1: Traceback (most recent call final): File "C: \ Python27 \ lib \ threading.py", line 810, The __bootstrap_inner file in the self () file "c: \ Users \ myuser \ workspace \ new \ scriptt.py", line 89, in the self.calculate () file "Q: \ users \ myuser \ workspace \ new \ scriptt. Py ", line 93, in the calculation of self.message_type, Self.new_date, self.old_code, AttributeError: 'ReaderThread' in the object E attribute 'message_type' is not   

What's wrong with the above?

The description of the problem is quite difficult because it does not really appear in the code that you have shown to us But I can guess what your actual code looks like, and how to fix this estimate.


Traceback says:

  file "user \ myuser \ workspace \ new \ scriptt.py", line 93, calculate self. Message_type, self.new_date, self.old_code, AttributeError: There is no attribute 'message_type' in the 'ReaderThread' object  

but there is no such line in your code. The closest thing is this:

  self.message_type, self.new_date, self.old_code = self.message  

This line is completely valid - this is an attempt to For the set message_type

The line you are getting an error is not trying to set it, it only access and because of this A characteristic of them still does not exist, you get a attributererror .


Almost certainly, in your real code, you are doing something like this:

  self.message_type, self .new_date, self.old_code, self.fifth_attribute = self.message  

You can see that you like the same statement, just like the example you showed us- but it's not like that. There are very strict rules about Python (contrast, say, C or Javascript) when a statement continues on the next line, and in any other case, the end of the sentence ends at the end of the line.

The first line is a complete statement in itself: A simple expression statement, where expression is a tuple of three values ​​as soon as you enter 1, 2, 3 Or 1, 2, 3, and see (1, 2, 3) , you can type self.message_type, self.new_date, Self.old_code, and this will evaluate tuple (self.message_type, self.new_date, self.old_code) . Generally, your code will be useless and deceptive, but harmless; In this case, because you have not actually created these features yet, you get a attributererror .

The second line is definitely a normal assignment statement, which is itself. in self.other_attribute and self.fifth_attribute (probably increase ValueError to open several elements, but


The next line is the simplest and most common rule for continuing an expression, when there is no unrecognized parenthesis, square brackets, or braces, and you can take advantage of it here, because you can effectively He is specifying the Tupal, and the Tupelle can always be written inside the bracket. For:

  (self.message_type, self.new_date, self.old_code, self.other_attribute, self.fifth_attribute) = self.message  

* I'm cheating a little bit in explanation because the thing on the left side of the job is not really an expression, and in particular there is not actually a tuple, but an assignment target list But the rules have been carefully designed to ensure that a list of manifest form forms of expression Sub-section in terms of the exact syntax, see the docs.


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -