-
-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug:the call __init__ failed #103
Conversation
@@ -220,7 +220,8 @@ async def load_state(cls, fname, port, interface='0.0.0.0'): | |||
log.info("Loading state from %s", fname) | |||
with open(fname, 'rb') as file: | |||
data = pickle.load(file) | |||
svr = Server(data['ksize'], data['alpha'], data['id']) | |||
svr = eval(cls.__name__+"(data['ksize'], data['alpha'], data['id'])") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this not work?
svr = eval(cls.__name__+"(data['ksize'], data['alpha'], data['id'])") | |
svr = cls(data['ksize'], data['alpha'], data['id']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, my English is poor, so my responses and these are all machine translation
When you inherit this class, because the referenced constructor is still the inherited class
So if you modify the constructor of the inherited class, the modified constructor will not work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this not work?
svr = eval(cls.__name__+"(data['ksize'], data['alpha'], data['id'])") | |
svr = eval(cls.__class__.__name__+"(data['ksize'], data['alpha'], data['id'])") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the first version, I overlooked that there may not be name properties, modified here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class test1:
def __init__(self):
print("test1")
def save(self):
test1()
class test2:
def __init__(self):
print("test2")
def save(self):
eval(self.__class__.__name__+"()")
class test3(test1):
def __init__(self):
print("test3")
class test4(test2):
def __init__(self):
print("test4")
test1()
a=test2()
a.save()
a=test3()
a.save()
a=test4()
a.save()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this not work?
Hope you can figure it out with this example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this not work?
And your changes are incomplete, because there is a possibility that the error will be reported because there is no call
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think your example matches this scenario - the load_state
method is a @classmethod
. Here's an example that I fits a bit better, and demonstrates that even a class that extends the base Server
can be successfully instantiated in the load_state
:
class Server:
def __init__(self, details):
self.details = details
@classmethod
def load_state(cls, details):
svr = cls(details)
return svr
class ExtendingServer(Server):
def test(self):
print('hello %s' % self.details)
s = ExtendingServer.load_state('there')
s.test()
When this code was written, it wasn't assumed that anyone would be extending Server
- but I don't mind supporting that potential case.
Closed in favor of ff19ddb |
bug:Calling a load_state method after inheriting the kademlia.network.Server class causes the inherited and modified init function to fail to act on the return value of the load_state method
fixed:Get the inherited class name and call the constructor via eval().