I have this python 3 code:
with next(val for key,val in MyServer.my_dict.items() if 'AS1' in key) as AS:
AS.port = 80
AS.connect()
...
I am getting an 'AttributeError:__enter__'
. What does this mean? Is there a reason this will not work? Or is there a better way to do this?
EDIT:
I found THIS, which appears to be the answer. I just added the __enter__
and __exit__
methods after the __init__
of Server (the class of MyServer), and it worked:
class Server:
"""Base server class"""
def __init__(self, ip_addr, port, username, password):
self.ip_addr = ip_addr
self.port = port
self.user = username
self.p_word = password
self.name = 'Generic Server'
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
pass
Is there something else I need to do? Is that it?