0

I have a class which is declared like:

class K1:
  
    S_LENGTH = 1600
    SUFFIX = 0x06   

    def __init__(self,variant):
        self.__state_bytes_length = self.S_LENGTH
        self.__delimited_suffix = self.SUFFIX
        self.R_LENGTH = self.variant
        CAP_LENGTH = self.S_LENGTH - self.R_LENGTH
        self.H_LENGTH = CAP_LENGTH/2
        self.__rate_bytes_length = self.R_LENGTH
        self.__hash_bytes_length = self.H_LENGTH 

        self.__state_in_bytes = bytearray([0 for i in range(self.__state_bytes_length)])
        self.__capacity_bytes_length = self.__state_bytes_length - self.__rate_bytes_length
        self.__hash_bytes = bytearray()

I want to take an input value (called variant in my code) from the user and then assign that value to R_LENGTH. When I try to declare it in the aforementioned manner, it throws an error:

AttributeError: 'K1' object has no attribute 'variant'

I am unable to figure out why this error occurs.

6
  • 1
    You haven't defined variant when you do R_LENGTH = self.variant. Also indentation of those class variables look wrong. Commented Oct 11, 2021 at 6:33
  • 1
    I think you want R_LENGTH = variant, without the self. You may have a similar issue with self.__SHA3_HASH_LENGTH later, since that's not a defined attribute either. I'm not sure how to solve that one. Commented Oct 11, 2021 at 6:34
  • 1
    CAP_LENGTH is not defined. So you need to post a minimal reproducible example instead of half working code. Commented Oct 11, 2021 at 6:35
  • 2
    It might help your troubleshooting to get rid of the try and except blocks you show in your calling code. If you let an exception bubble out uncaught, the interpreter will generally show you a traceback which identifies exactly where the error occurred. That's useful information, don't throw it away unless you have a good reason! Commented Oct 11, 2021 at 6:37
  • 1
    Kudos for updating the question. Your code still generate syntax errors for the S_LENGTH and SUFFIX assignments. In the line CAP_LENGTH = S_LENGTH - R_LENGTH is another syntax error, either use K1.S_LENGTH or self.S_LENGTH as you did earlier. You need to post working code, otherwise you are just wasting our time. Commented Oct 11, 2021 at 7:31

1 Answer 1

1

The error AttributeError: 'K1' object has no attribute 'variant' is due to the statement R_LENGTH = self.variant when self.variant has not been defined yet. As @Blckknght said you probably want to use just variant.

I don't understand why are you are using class static data (S_LENGTH, SUFFIX). In either case the posted code doesn't run as is as is so I used 0 as place holders:

class K1:
    S_LENGTH = 0
    SUFFIX = 0

    def __init__(self, variant):
        self.__state_bytes_length = self.S_LENGTH
        self.__delimited_suffix = self.SUFFIX
        self.__hash_bytes_length = (self.S_LENGTH - variant) / 2


k_obj = K1(42)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.