OOP / try except __init__ of class 中的语句
OOP / try except statements in __init__ of class
我想控制classWebpage
的输入。意思是,我想确保适当地提供了网页的 link,例如'http://example.com'
。
class Webpage(object):
def __init__(self, link):
prefix = ['http', 'https']
suffix = ['com', 'net']
try:
if link.split(':')[0] in prefix and link.split('.')[-1] in suffix:
self.link = link
except:
raise ValueError('Invalid link')
def get_link(self):
'''
Used to safely access self.link outside of the class
Returns: self.link
'''
return self.link
def __str__(self):
return str(self.link)
然而,当我尝试代码时:
test_link = Webpage('example.com')
我没有得到 ValueError
,正如我所期望的。方法调用:
test_link.get_link()
print(test_lint)
结果
AttributeError: 'Webpage' object has no attribute 'link'
这表示 try/except 部分工作 - try
没有执行 self.link = link
,但是 except
语句没有执行。
一个例子:
test_link = Webpage('http://example.com')
与 class 的 get_link()
和 print
方法配合使用效果很好。
感谢任何提示。
在 try 块中提出 ValueError 的期望值,并在 except 块中处理期望值
更多信息请访问Raising Expections in python
class Webpage(object):
def __init__(self, link):
prefix = ['http', 'https']
suffix = ['com', 'net']
try:
if link.split(':')[0] in prefix and link.split('.')[-1] in suffix:
self.link = link
else:
raise ValueError('Invalid link')
except ValueError as exp:
print("the value error is {}\nthe link specified is {} ".format(exp,link))
def get_link(self):
'''
Used to safely access self.link outside of the class
Returns: self.link
'''
return self.link
def __str__(self):
return str(self.link)
test_link = Webpage('example.com')
输出
the value error is Invalid link
the link specified is example.com
希望对您有所帮助
try:
if link.split(':')[0] in prefix and link.split('.')[-1] in suffix:
self.link = link
except:
raise ValueError('Invalid link')
如果您传递 link example.com
,if 语句失败,因为它不包含任何前面提到的前缀。由于它在逻辑上是正确的,它永远不会下降到 except
块。
您可能想检查 self.link
是否存在于 get_link
函数
中
试试这个更新后的代码
class Webpage(object):
def __init__(self, link):
prefix = ['http', 'https']
suffix = ['com', 'net']
if link.split(':')[0] in prefix and link.split('.')[-1] in suffix:
self.link = link
else:
self.link = 'Invalid link'
def get_link(self):
'''
Used to safely access self.link outside of the class
Returns: self.link
'''
return self.link
def __str__(self):
return str(self.link)
test_link = Webpage('example.com')
test_link.get_link()
print(test_link)
您可以使用 str.startswith
和 str.endswith
并在 else 中创建 raise
。
演示:
class Webpage(object):
def __init__(self, link):
prefix = ('http', 'https')
suffix = ('com', 'net')
if (link.startswith(prefix)) and (link.endswith(suffix)):
self.link = link
else:
raise ValueError('Invalid link')
def get_link(self):
'''
Used to safely access self.link outside of the class
Returns: self.link
'''
return self.link
def __str__(self):
return str(self.link)
test_link = Webpage( 'example.com')
print(test_link.get_link())
我想控制classWebpage
的输入。意思是,我想确保适当地提供了网页的 link,例如'http://example.com'
。
class Webpage(object):
def __init__(self, link):
prefix = ['http', 'https']
suffix = ['com', 'net']
try:
if link.split(':')[0] in prefix and link.split('.')[-1] in suffix:
self.link = link
except:
raise ValueError('Invalid link')
def get_link(self):
'''
Used to safely access self.link outside of the class
Returns: self.link
'''
return self.link
def __str__(self):
return str(self.link)
然而,当我尝试代码时:
test_link = Webpage('example.com')
我没有得到 ValueError
,正如我所期望的。方法调用:
test_link.get_link()
print(test_lint)
结果
AttributeError: 'Webpage' object has no attribute 'link'
这表示 try/except 部分工作 - try
没有执行 self.link = link
,但是 except
语句没有执行。
一个例子:
test_link = Webpage('http://example.com')
与 class 的 get_link()
和 print
方法配合使用效果很好。
感谢任何提示。
在 try 块中提出 ValueError 的期望值,并在 except 块中处理期望值
更多信息请访问Raising Expections in python
class Webpage(object):
def __init__(self, link):
prefix = ['http', 'https']
suffix = ['com', 'net']
try:
if link.split(':')[0] in prefix and link.split('.')[-1] in suffix:
self.link = link
else:
raise ValueError('Invalid link')
except ValueError as exp:
print("the value error is {}\nthe link specified is {} ".format(exp,link))
def get_link(self):
'''
Used to safely access self.link outside of the class
Returns: self.link
'''
return self.link
def __str__(self):
return str(self.link)
test_link = Webpage('example.com')
输出
the value error is Invalid link
the link specified is example.com
希望对您有所帮助
try:
if link.split(':')[0] in prefix and link.split('.')[-1] in suffix:
self.link = link
except:
raise ValueError('Invalid link')
如果您传递 link example.com
,if 语句失败,因为它不包含任何前面提到的前缀。由于它在逻辑上是正确的,它永远不会下降到 except
块。
您可能想检查 self.link
是否存在于 get_link
函数
试试这个更新后的代码
class Webpage(object):
def __init__(self, link):
prefix = ['http', 'https']
suffix = ['com', 'net']
if link.split(':')[0] in prefix and link.split('.')[-1] in suffix:
self.link = link
else:
self.link = 'Invalid link'
def get_link(self):
'''
Used to safely access self.link outside of the class
Returns: self.link
'''
return self.link
def __str__(self):
return str(self.link)
test_link = Webpage('example.com')
test_link.get_link()
print(test_link)
您可以使用 str.startswith
和 str.endswith
并在 else 中创建 raise
。
演示:
class Webpage(object):
def __init__(self, link):
prefix = ('http', 'https')
suffix = ('com', 'net')
if (link.startswith(prefix)) and (link.endswith(suffix)):
self.link = link
else:
raise ValueError('Invalid link')
def get_link(self):
'''
Used to safely access self.link outside of the class
Returns: self.link
'''
return self.link
def __str__(self):
return str(self.link)
test_link = Webpage( 'example.com')
print(test_link.get_link())