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?
2 Respostas
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
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