0

How to make an string attribute of the given length?

I need to make a class, every instance of which has an obligatory string attribute named 'str_attr' of length precisely 10. How can I do it in python?

14th Mar 2019, 6:03 PM
Vladislav Riabets
Vladislav Riabets - avatar
2 Antworten
0
Something like this? class Test: str_attr = "abcdefghij" def __init__(self, age): self.age = age alice = Test(42) bob = Test(50) print(len(alice.str_attr)) # 10 print(len(bob.str_attr)) # 10 print(alice.age) # 42 print(bob.age) # 50
14th Mar 2019, 10:32 PM
Diego
Diego - avatar
0
I found a decision. Meant this: class Myslass: def __init__(self, str_attr): if len(str_attr) != 10: raise ValueError("wrong length") self.str_attr = str_attr
16th Mar 2019, 9:02 AM
Vladislav Riabets
Vladislav Riabets - avatar