How to create an object specific list that doesn't empty itself when called more than once
My problem
in init I declare two list variables that are called nlist
and rlist
.
Then in method add_neighour
I am appending object (router2) named router2.__routername
to the list of object called self.nlist
.
class Router:
def __init__(self, routername):
self.__routername = routername
self.nlist =
self.rlist =
def print_info(self):
print(self.nlist)
print(" ", self.__routername, sep="")
print(" N: ", ", ".join(sorted(self.nlist)), sep="")
print(" R: ", ", ".join(sorted(self.rlist)), sep="")
def add_neighbour(self, router2):
self.nlist.append(router2.__routername)
print(self.nlist)
I'm calling the code inside the while True that waits for a specific commmand:
Command P is for printing info
if command == "P":
routername = input("Enter router name: ")
if routername not in mydict:
print("Router was not found.")
else:
router = Router(routername)
router.print_info()
Command C is for chaining two routers together. (for example appending router2s name router2.__routername
to the neighbour list self.nlist
of router1.
This happens class Router
inside the method add_neighbour
that takes another router as parameter.
elif command == "C":
router1 = input("Enter 1st router: ")
router2 = input("Enter 2nd router: ")
router1 = Router(router1)
router2 = Router(router2)
router1.add_neighbour(router2)
router2.add_neighbour(router1)
The appending goes fine and when I print(self.nlist)
at the end it shows up that the list has been updated with the appended element I wanted. However, when calling the print_info
function and trying to print the sorted(self.nlist)
It prints an empty list.
I have no clue why. Should I declare the object specific list in a different way/place?
or
append values to the list differently?
my goal is to: succesfully print self.nlist
inside the print_info
method. Succesfully printing meaning that it prints something else than an empty list.
I tried looking for answers from several posts. Sorry if this post is duplicate and horribly written.
The whole code: (in progress as you can see...)
class Router:
def __init__(self, routername):
self.__routername = routername
self.nlist =
self.rlist =
def print_info(self):
print(self.nlist)
print(" ", self.__routername, sep="")
print(" N: ", ", ".join(sorted(self.nlist)), sep="")
print(" R: ", ", ".join(sorted(self.rlist)), sep="")
def add_neighbour(self, router2):
self.nlist.append(router2.__routername)
print(self.nlist)
def main():
mydict = {}
routerfile = input("Network file: ")
while True:
command = input("> ")
command = command.upper()
if command == "P":
routername = input("Enter router name: ")
if routername not in mydict:
print("Router was not found.")
else:
router = Router(routername)
router.print_info()
pass
elif command == "PA":
pass
elif command == "S":
pass
elif command == "C":
router1 = input("Enter 1st router: ")
router2 = input("Enter 2nd router: ")
router1 = Router(router1)
router2 = Router(router2)
router1.add_neighbour(router2)
router2.add_neighbour(router1)
pass
elif command == "RR":
pass
elif command == "NR":
routername = input("Enter a new name: ")
router = Router(routername)
if routername not in mydict:
mydict[routername] = router
else:
print("Name is taken.")
pass
elif command == "NN":
pass
elif command == "Q":
print("Simulator closes.")
return
else:
print("Erroneous command!")
print("Enter one of these commands:")
print("NR (new router)")
print("P (print)")
print("C (connect)")
print("NN (new network)")
print("PA (print all)")
print("S (send routing tables)")
print("RR (route request)")
print("Q (quit)")
main()
python class pycharm
add a comment |
My problem
in init I declare two list variables that are called nlist
and rlist
.
Then in method add_neighour
I am appending object (router2) named router2.__routername
to the list of object called self.nlist
.
class Router:
def __init__(self, routername):
self.__routername = routername
self.nlist =
self.rlist =
def print_info(self):
print(self.nlist)
print(" ", self.__routername, sep="")
print(" N: ", ", ".join(sorted(self.nlist)), sep="")
print(" R: ", ", ".join(sorted(self.rlist)), sep="")
def add_neighbour(self, router2):
self.nlist.append(router2.__routername)
print(self.nlist)
I'm calling the code inside the while True that waits for a specific commmand:
Command P is for printing info
if command == "P":
routername = input("Enter router name: ")
if routername not in mydict:
print("Router was not found.")
else:
router = Router(routername)
router.print_info()
Command C is for chaining two routers together. (for example appending router2s name router2.__routername
to the neighbour list self.nlist
of router1.
This happens class Router
inside the method add_neighbour
that takes another router as parameter.
elif command == "C":
router1 = input("Enter 1st router: ")
router2 = input("Enter 2nd router: ")
router1 = Router(router1)
router2 = Router(router2)
router1.add_neighbour(router2)
router2.add_neighbour(router1)
The appending goes fine and when I print(self.nlist)
at the end it shows up that the list has been updated with the appended element I wanted. However, when calling the print_info
function and trying to print the sorted(self.nlist)
It prints an empty list.
I have no clue why. Should I declare the object specific list in a different way/place?
or
append values to the list differently?
my goal is to: succesfully print self.nlist
inside the print_info
method. Succesfully printing meaning that it prints something else than an empty list.
I tried looking for answers from several posts. Sorry if this post is duplicate and horribly written.
The whole code: (in progress as you can see...)
class Router:
def __init__(self, routername):
self.__routername = routername
self.nlist =
self.rlist =
def print_info(self):
print(self.nlist)
print(" ", self.__routername, sep="")
print(" N: ", ", ".join(sorted(self.nlist)), sep="")
print(" R: ", ", ".join(sorted(self.rlist)), sep="")
def add_neighbour(self, router2):
self.nlist.append(router2.__routername)
print(self.nlist)
def main():
mydict = {}
routerfile = input("Network file: ")
while True:
command = input("> ")
command = command.upper()
if command == "P":
routername = input("Enter router name: ")
if routername not in mydict:
print("Router was not found.")
else:
router = Router(routername)
router.print_info()
pass
elif command == "PA":
pass
elif command == "S":
pass
elif command == "C":
router1 = input("Enter 1st router: ")
router2 = input("Enter 2nd router: ")
router1 = Router(router1)
router2 = Router(router2)
router1.add_neighbour(router2)
router2.add_neighbour(router1)
pass
elif command == "RR":
pass
elif command == "NR":
routername = input("Enter a new name: ")
router = Router(routername)
if routername not in mydict:
mydict[routername] = router
else:
print("Name is taken.")
pass
elif command == "NN":
pass
elif command == "Q":
print("Simulator closes.")
return
else:
print("Erroneous command!")
print("Enter one of these commands:")
print("NR (new router)")
print("P (print)")
print("C (connect)")
print("NN (new network)")
print("PA (print all)")
print("S (send routing tables)")
print("RR (route request)")
print("Q (quit)")
main()
python class pycharm
Please show how you are calling this code, with both the add_neighbour and print_info calls.
– Daniel Roseman
Nov 20 '18 at 13:22
Note also, this code as you have posted it would give an AttributeError in add_neighbour, because__routername
will be mangled due to the double-underscore. Please show actual code.
– Daniel Roseman
Nov 20 '18 at 13:27
All of your commands are creating entirely newRouter
objects, each with an initially emptynlist
. Of course that prints out as empty - the objects to which you previously added neighbors have long since been discarded.
– jasonharper
Nov 20 '18 at 14:12
add a comment |
My problem
in init I declare two list variables that are called nlist
and rlist
.
Then in method add_neighour
I am appending object (router2) named router2.__routername
to the list of object called self.nlist
.
class Router:
def __init__(self, routername):
self.__routername = routername
self.nlist =
self.rlist =
def print_info(self):
print(self.nlist)
print(" ", self.__routername, sep="")
print(" N: ", ", ".join(sorted(self.nlist)), sep="")
print(" R: ", ", ".join(sorted(self.rlist)), sep="")
def add_neighbour(self, router2):
self.nlist.append(router2.__routername)
print(self.nlist)
I'm calling the code inside the while True that waits for a specific commmand:
Command P is for printing info
if command == "P":
routername = input("Enter router name: ")
if routername not in mydict:
print("Router was not found.")
else:
router = Router(routername)
router.print_info()
Command C is for chaining two routers together. (for example appending router2s name router2.__routername
to the neighbour list self.nlist
of router1.
This happens class Router
inside the method add_neighbour
that takes another router as parameter.
elif command == "C":
router1 = input("Enter 1st router: ")
router2 = input("Enter 2nd router: ")
router1 = Router(router1)
router2 = Router(router2)
router1.add_neighbour(router2)
router2.add_neighbour(router1)
The appending goes fine and when I print(self.nlist)
at the end it shows up that the list has been updated with the appended element I wanted. However, when calling the print_info
function and trying to print the sorted(self.nlist)
It prints an empty list.
I have no clue why. Should I declare the object specific list in a different way/place?
or
append values to the list differently?
my goal is to: succesfully print self.nlist
inside the print_info
method. Succesfully printing meaning that it prints something else than an empty list.
I tried looking for answers from several posts. Sorry if this post is duplicate and horribly written.
The whole code: (in progress as you can see...)
class Router:
def __init__(self, routername):
self.__routername = routername
self.nlist =
self.rlist =
def print_info(self):
print(self.nlist)
print(" ", self.__routername, sep="")
print(" N: ", ", ".join(sorted(self.nlist)), sep="")
print(" R: ", ", ".join(sorted(self.rlist)), sep="")
def add_neighbour(self, router2):
self.nlist.append(router2.__routername)
print(self.nlist)
def main():
mydict = {}
routerfile = input("Network file: ")
while True:
command = input("> ")
command = command.upper()
if command == "P":
routername = input("Enter router name: ")
if routername not in mydict:
print("Router was not found.")
else:
router = Router(routername)
router.print_info()
pass
elif command == "PA":
pass
elif command == "S":
pass
elif command == "C":
router1 = input("Enter 1st router: ")
router2 = input("Enter 2nd router: ")
router1 = Router(router1)
router2 = Router(router2)
router1.add_neighbour(router2)
router2.add_neighbour(router1)
pass
elif command == "RR":
pass
elif command == "NR":
routername = input("Enter a new name: ")
router = Router(routername)
if routername not in mydict:
mydict[routername] = router
else:
print("Name is taken.")
pass
elif command == "NN":
pass
elif command == "Q":
print("Simulator closes.")
return
else:
print("Erroneous command!")
print("Enter one of these commands:")
print("NR (new router)")
print("P (print)")
print("C (connect)")
print("NN (new network)")
print("PA (print all)")
print("S (send routing tables)")
print("RR (route request)")
print("Q (quit)")
main()
python class pycharm
My problem
in init I declare two list variables that are called nlist
and rlist
.
Then in method add_neighour
I am appending object (router2) named router2.__routername
to the list of object called self.nlist
.
class Router:
def __init__(self, routername):
self.__routername = routername
self.nlist =
self.rlist =
def print_info(self):
print(self.nlist)
print(" ", self.__routername, sep="")
print(" N: ", ", ".join(sorted(self.nlist)), sep="")
print(" R: ", ", ".join(sorted(self.rlist)), sep="")
def add_neighbour(self, router2):
self.nlist.append(router2.__routername)
print(self.nlist)
I'm calling the code inside the while True that waits for a specific commmand:
Command P is for printing info
if command == "P":
routername = input("Enter router name: ")
if routername not in mydict:
print("Router was not found.")
else:
router = Router(routername)
router.print_info()
Command C is for chaining two routers together. (for example appending router2s name router2.__routername
to the neighbour list self.nlist
of router1.
This happens class Router
inside the method add_neighbour
that takes another router as parameter.
elif command == "C":
router1 = input("Enter 1st router: ")
router2 = input("Enter 2nd router: ")
router1 = Router(router1)
router2 = Router(router2)
router1.add_neighbour(router2)
router2.add_neighbour(router1)
The appending goes fine and when I print(self.nlist)
at the end it shows up that the list has been updated with the appended element I wanted. However, when calling the print_info
function and trying to print the sorted(self.nlist)
It prints an empty list.
I have no clue why. Should I declare the object specific list in a different way/place?
or
append values to the list differently?
my goal is to: succesfully print self.nlist
inside the print_info
method. Succesfully printing meaning that it prints something else than an empty list.
I tried looking for answers from several posts. Sorry if this post is duplicate and horribly written.
The whole code: (in progress as you can see...)
class Router:
def __init__(self, routername):
self.__routername = routername
self.nlist =
self.rlist =
def print_info(self):
print(self.nlist)
print(" ", self.__routername, sep="")
print(" N: ", ", ".join(sorted(self.nlist)), sep="")
print(" R: ", ", ".join(sorted(self.rlist)), sep="")
def add_neighbour(self, router2):
self.nlist.append(router2.__routername)
print(self.nlist)
def main():
mydict = {}
routerfile = input("Network file: ")
while True:
command = input("> ")
command = command.upper()
if command == "P":
routername = input("Enter router name: ")
if routername not in mydict:
print("Router was not found.")
else:
router = Router(routername)
router.print_info()
pass
elif command == "PA":
pass
elif command == "S":
pass
elif command == "C":
router1 = input("Enter 1st router: ")
router2 = input("Enter 2nd router: ")
router1 = Router(router1)
router2 = Router(router2)
router1.add_neighbour(router2)
router2.add_neighbour(router1)
pass
elif command == "RR":
pass
elif command == "NR":
routername = input("Enter a new name: ")
router = Router(routername)
if routername not in mydict:
mydict[routername] = router
else:
print("Name is taken.")
pass
elif command == "NN":
pass
elif command == "Q":
print("Simulator closes.")
return
else:
print("Erroneous command!")
print("Enter one of these commands:")
print("NR (new router)")
print("P (print)")
print("C (connect)")
print("NN (new network)")
print("PA (print all)")
print("S (send routing tables)")
print("RR (route request)")
print("Q (quit)")
main()
python class pycharm
python class pycharm
edited Nov 20 '18 at 13:38
Riku Rainio
asked Nov 20 '18 at 13:21
Riku RainioRiku Rainio
12
12
Please show how you are calling this code, with both the add_neighbour and print_info calls.
– Daniel Roseman
Nov 20 '18 at 13:22
Note also, this code as you have posted it would give an AttributeError in add_neighbour, because__routername
will be mangled due to the double-underscore. Please show actual code.
– Daniel Roseman
Nov 20 '18 at 13:27
All of your commands are creating entirely newRouter
objects, each with an initially emptynlist
. Of course that prints out as empty - the objects to which you previously added neighbors have long since been discarded.
– jasonharper
Nov 20 '18 at 14:12
add a comment |
Please show how you are calling this code, with both the add_neighbour and print_info calls.
– Daniel Roseman
Nov 20 '18 at 13:22
Note also, this code as you have posted it would give an AttributeError in add_neighbour, because__routername
will be mangled due to the double-underscore. Please show actual code.
– Daniel Roseman
Nov 20 '18 at 13:27
All of your commands are creating entirely newRouter
objects, each with an initially emptynlist
. Of course that prints out as empty - the objects to which you previously added neighbors have long since been discarded.
– jasonharper
Nov 20 '18 at 14:12
Please show how you are calling this code, with both the add_neighbour and print_info calls.
– Daniel Roseman
Nov 20 '18 at 13:22
Please show how you are calling this code, with both the add_neighbour and print_info calls.
– Daniel Roseman
Nov 20 '18 at 13:22
Note also, this code as you have posted it would give an AttributeError in add_neighbour, because
__routername
will be mangled due to the double-underscore. Please show actual code.– Daniel Roseman
Nov 20 '18 at 13:27
Note also, this code as you have posted it would give an AttributeError in add_neighbour, because
__routername
will be mangled due to the double-underscore. Please show actual code.– Daniel Roseman
Nov 20 '18 at 13:27
All of your commands are creating entirely new
Router
objects, each with an initially empty nlist
. Of course that prints out as empty - the objects to which you previously added neighbors have long since been discarded.– jasonharper
Nov 20 '18 at 14:12
All of your commands are creating entirely new
Router
objects, each with an initially empty nlist
. Of course that prints out as empty - the objects to which you previously added neighbors have long since been discarded.– jasonharper
Nov 20 '18 at 14:12
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393931%2fhow-to-create-an-object-specific-list-that-doesnt-empty-itself-when-called-more%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393931%2fhow-to-create-an-object-specific-list-that-doesnt-empty-itself-when-called-more%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please show how you are calling this code, with both the add_neighbour and print_info calls.
– Daniel Roseman
Nov 20 '18 at 13:22
Note also, this code as you have posted it would give an AttributeError in add_neighbour, because
__routername
will be mangled due to the double-underscore. Please show actual code.– Daniel Roseman
Nov 20 '18 at 13:27
All of your commands are creating entirely new
Router
objects, each with an initially emptynlist
. Of course that prints out as empty - the objects to which you previously added neighbors have long since been discarded.– jasonharper
Nov 20 '18 at 14:12