0

I am a newbie in Python and playing around with the classes right now,

Have a look at this simple code,

class Testing:

    BB_Key_Length = {256 : 240}

#method __init__() is a special method, which is called class constructor or initialization method that 
#Python calls when you create a new instance of this class.    
    def __init__(self, key_length):
        self._key_length = key_length
        self._n = int(key_length / 8)

        if key_length in self.BB_Key_Length:
            self._b = self.BB_Key_Length[key_length]
            print(self._b)


Object1 = Testing(200)
print(Testing.BB_Key_Length)

on line 13, it is written that, print(self._b) which is also inside the __init__ function but why the value of self._b is not printing when I am creating the object

Object1 = Testing(200)

All I want is to print the value of self._b which i couldn't be able to print

3
  • 2
    Because of the line if key_length in self.BB_Key_Length. If that condition isn't met, the line print(self._b) never happens. And indeed, key_length will be equal to 200, and 200 is not in the dictionary {256 : 240} Commented Oct 8, 2013 at 18:30
  • but 200 is in range of 240 and 256..Why it is not printing then Commented Oct 8, 2013 at 18:33
  • 1
    That's not how dictionaries work. if 200 in self.BB_Key_Length just checks whether there is a key in self.BB_Key_Length equal to 200. But there is only one key in self.BB_Key_Length, and that is 256. Commented Oct 8, 2013 at 18:34

1 Answer 1

1

why the value of self._b is not printing when I am creating the object Object1 = Testing(200)

Because the print statement is inside an if statement, which is false because 200 is not a key in the dict self.BB_Key_Length.

Sign up to request clarification or add additional context in comments.

4 Comments

but 200 is in range of 240 and 256..Why it is not printing then
@Xufyan That's not a range. That's a dict. If you want a range, use a range.
@Xufyan Also, 200 is not in the range (240,256).
@Xufyan, a dict is a mapping, function, a container that for each key A returns an associated value B. In your case, you have defined a dictionary (dict) with a single key 256 of value 240

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.