Adding object into a list with function
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Is it possible to write a function inside class myClass that will add created objects into a list?
I have wrote def addInList(self): but I do not know how to get a created object so that I can append it into a list.
I want to have a program that will create an object when you call a function, and that will automaticly append it into a list.
This is the code:
class myClass:
def __init__(self, name, city):
self.name = name
self.city = city
def addInList(self):
pass
def __str__(self):
return "Name: {}nCity: {}n".format(self.name, self.city)
First = myClass("Mike","New York")
Second = myClass("Steve", "Los Angeles")
print(First)
print(Second)
I want to have a list of objects, so that when I print the list i get list of all created objects.
python oop
add a comment |
Is it possible to write a function inside class myClass that will add created objects into a list?
I have wrote def addInList(self): but I do not know how to get a created object so that I can append it into a list.
I want to have a program that will create an object when you call a function, and that will automaticly append it into a list.
This is the code:
class myClass:
def __init__(self, name, city):
self.name = name
self.city = city
def addInList(self):
pass
def __str__(self):
return "Name: {}nCity: {}n".format(self.name, self.city)
First = myClass("Mike","New York")
Second = myClass("Steve", "Los Angeles")
print(First)
print(Second)
I want to have a list of objects, so that when I print the list i get list of all created objects.
python oop
1
It's unclear what you want exactly. Provide some input and expected output.
– SilverSlash
Nov 16 '18 at 17:38
I updated the question
– Void Beats
Nov 16 '18 at 18:06
>>I want to have a program that will create an object when you call a function, and that will automatically append it into a list.<< Correct me if I am wrong but you want to have an object that has a method that will create this object itself?
– artona
Nov 16 '18 at 19:08
add a comment |
Is it possible to write a function inside class myClass that will add created objects into a list?
I have wrote def addInList(self): but I do not know how to get a created object so that I can append it into a list.
I want to have a program that will create an object when you call a function, and that will automaticly append it into a list.
This is the code:
class myClass:
def __init__(self, name, city):
self.name = name
self.city = city
def addInList(self):
pass
def __str__(self):
return "Name: {}nCity: {}n".format(self.name, self.city)
First = myClass("Mike","New York")
Second = myClass("Steve", "Los Angeles")
print(First)
print(Second)
I want to have a list of objects, so that when I print the list i get list of all created objects.
python oop
Is it possible to write a function inside class myClass that will add created objects into a list?
I have wrote def addInList(self): but I do not know how to get a created object so that I can append it into a list.
I want to have a program that will create an object when you call a function, and that will automaticly append it into a list.
This is the code:
class myClass:
def __init__(self, name, city):
self.name = name
self.city = city
def addInList(self):
pass
def __str__(self):
return "Name: {}nCity: {}n".format(self.name, self.city)
First = myClass("Mike","New York")
Second = myClass("Steve", "Los Angeles")
print(First)
print(Second)
I want to have a list of objects, so that when I print the list i get list of all created objects.
python oop
python oop
edited Nov 23 '18 at 11:23
Void Beats
asked Nov 16 '18 at 17:35
Void BeatsVoid Beats
2110
2110
1
It's unclear what you want exactly. Provide some input and expected output.
– SilverSlash
Nov 16 '18 at 17:38
I updated the question
– Void Beats
Nov 16 '18 at 18:06
>>I want to have a program that will create an object when you call a function, and that will automatically append it into a list.<< Correct me if I am wrong but you want to have an object that has a method that will create this object itself?
– artona
Nov 16 '18 at 19:08
add a comment |
1
It's unclear what you want exactly. Provide some input and expected output.
– SilverSlash
Nov 16 '18 at 17:38
I updated the question
– Void Beats
Nov 16 '18 at 18:06
>>I want to have a program that will create an object when you call a function, and that will automatically append it into a list.<< Correct me if I am wrong but you want to have an object that has a method that will create this object itself?
– artona
Nov 16 '18 at 19:08
1
1
It's unclear what you want exactly. Provide some input and expected output.
– SilverSlash
Nov 16 '18 at 17:38
It's unclear what you want exactly. Provide some input and expected output.
– SilverSlash
Nov 16 '18 at 17:38
I updated the question
– Void Beats
Nov 16 '18 at 18:06
I updated the question
– Void Beats
Nov 16 '18 at 18:06
>>I want to have a program that will create an object when you call a function, and that will automatically append it into a list.<< Correct me if I am wrong but you want to have an object that has a method that will create this object itself?
– artona
Nov 16 '18 at 19:08
>>I want to have a program that will create an object when you call a function, and that will automatically append it into a list.<< Correct me if I am wrong but you want to have an object that has a method that will create this object itself?
– artona
Nov 16 '18 at 19:08
add a comment |
2 Answers
2
active
oldest
votes
You can initialize a list in __init__(self) and append to this list in the method:
class myClass:
def __init__(self, name, city):
self.name = name
self.city = city
self.lst =
def addInList(self):
self.lst.extend([self.name, self.city])
Now, you can create objects like you did and call the function addInList(). <object>.lst gives the list created.
First = myClass("Mike","New York")
First.addInList()
print(First.lst)
# ['Mike', 'New York']
add a comment |
class myClass():
list_of_instances =
def __init__(self, name, city):
self.name = name
self.city = city
def __str__(self):
return "Name: {}nCity: {}n".format(self.name, self.city)
def addInList(self):
self.list_of_instances.append(self)
First = myClass("Mike","New York")
Second = myClass("Steve", "Los Angeles")
First.addInList()
print(First.list_of_instances)
# [<__main__.myClass at 0x7fee69cd6c18>]
Second.addInList()
for i in First.list_of_instances:
print(i)
Output:
Name: Mike
City: New York
Name: Steve
City: Los Angeles
Thanks my friend ! Is there a way to create an object with function?
– Void Beats
Nov 16 '18 at 17:56
@VoidBeats - please open new question or clarify this with required output
– artona
Nov 16 '18 at 18:27
add a comment |
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%2f53342797%2fadding-object-into-a-list-with-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can initialize a list in __init__(self) and append to this list in the method:
class myClass:
def __init__(self, name, city):
self.name = name
self.city = city
self.lst =
def addInList(self):
self.lst.extend([self.name, self.city])
Now, you can create objects like you did and call the function addInList(). <object>.lst gives the list created.
First = myClass("Mike","New York")
First.addInList()
print(First.lst)
# ['Mike', 'New York']
add a comment |
You can initialize a list in __init__(self) and append to this list in the method:
class myClass:
def __init__(self, name, city):
self.name = name
self.city = city
self.lst =
def addInList(self):
self.lst.extend([self.name, self.city])
Now, you can create objects like you did and call the function addInList(). <object>.lst gives the list created.
First = myClass("Mike","New York")
First.addInList()
print(First.lst)
# ['Mike', 'New York']
add a comment |
You can initialize a list in __init__(self) and append to this list in the method:
class myClass:
def __init__(self, name, city):
self.name = name
self.city = city
self.lst =
def addInList(self):
self.lst.extend([self.name, self.city])
Now, you can create objects like you did and call the function addInList(). <object>.lst gives the list created.
First = myClass("Mike","New York")
First.addInList()
print(First.lst)
# ['Mike', 'New York']
You can initialize a list in __init__(self) and append to this list in the method:
class myClass:
def __init__(self, name, city):
self.name = name
self.city = city
self.lst =
def addInList(self):
self.lst.extend([self.name, self.city])
Now, you can create objects like you did and call the function addInList(). <object>.lst gives the list created.
First = myClass("Mike","New York")
First.addInList()
print(First.lst)
# ['Mike', 'New York']
answered Nov 16 '18 at 17:45
AustinAustin
13.2k31031
13.2k31031
add a comment |
add a comment |
class myClass():
list_of_instances =
def __init__(self, name, city):
self.name = name
self.city = city
def __str__(self):
return "Name: {}nCity: {}n".format(self.name, self.city)
def addInList(self):
self.list_of_instances.append(self)
First = myClass("Mike","New York")
Second = myClass("Steve", "Los Angeles")
First.addInList()
print(First.list_of_instances)
# [<__main__.myClass at 0x7fee69cd6c18>]
Second.addInList()
for i in First.list_of_instances:
print(i)
Output:
Name: Mike
City: New York
Name: Steve
City: Los Angeles
Thanks my friend ! Is there a way to create an object with function?
– Void Beats
Nov 16 '18 at 17:56
@VoidBeats - please open new question or clarify this with required output
– artona
Nov 16 '18 at 18:27
add a comment |
class myClass():
list_of_instances =
def __init__(self, name, city):
self.name = name
self.city = city
def __str__(self):
return "Name: {}nCity: {}n".format(self.name, self.city)
def addInList(self):
self.list_of_instances.append(self)
First = myClass("Mike","New York")
Second = myClass("Steve", "Los Angeles")
First.addInList()
print(First.list_of_instances)
# [<__main__.myClass at 0x7fee69cd6c18>]
Second.addInList()
for i in First.list_of_instances:
print(i)
Output:
Name: Mike
City: New York
Name: Steve
City: Los Angeles
Thanks my friend ! Is there a way to create an object with function?
– Void Beats
Nov 16 '18 at 17:56
@VoidBeats - please open new question or clarify this with required output
– artona
Nov 16 '18 at 18:27
add a comment |
class myClass():
list_of_instances =
def __init__(self, name, city):
self.name = name
self.city = city
def __str__(self):
return "Name: {}nCity: {}n".format(self.name, self.city)
def addInList(self):
self.list_of_instances.append(self)
First = myClass("Mike","New York")
Second = myClass("Steve", "Los Angeles")
First.addInList()
print(First.list_of_instances)
# [<__main__.myClass at 0x7fee69cd6c18>]
Second.addInList()
for i in First.list_of_instances:
print(i)
Output:
Name: Mike
City: New York
Name: Steve
City: Los Angeles
class myClass():
list_of_instances =
def __init__(self, name, city):
self.name = name
self.city = city
def __str__(self):
return "Name: {}nCity: {}n".format(self.name, self.city)
def addInList(self):
self.list_of_instances.append(self)
First = myClass("Mike","New York")
Second = myClass("Steve", "Los Angeles")
First.addInList()
print(First.list_of_instances)
# [<__main__.myClass at 0x7fee69cd6c18>]
Second.addInList()
for i in First.list_of_instances:
print(i)
Output:
Name: Mike
City: New York
Name: Steve
City: Los Angeles
edited Nov 16 '18 at 17:47
answered Nov 16 '18 at 17:42
artonaartona
71248
71248
Thanks my friend ! Is there a way to create an object with function?
– Void Beats
Nov 16 '18 at 17:56
@VoidBeats - please open new question or clarify this with required output
– artona
Nov 16 '18 at 18:27
add a comment |
Thanks my friend ! Is there a way to create an object with function?
– Void Beats
Nov 16 '18 at 17:56
@VoidBeats - please open new question or clarify this with required output
– artona
Nov 16 '18 at 18:27
Thanks my friend ! Is there a way to create an object with function?
– Void Beats
Nov 16 '18 at 17:56
Thanks my friend ! Is there a way to create an object with function?
– Void Beats
Nov 16 '18 at 17:56
@VoidBeats - please open new question or clarify this with required output
– artona
Nov 16 '18 at 18:27
@VoidBeats - please open new question or clarify this with required output
– artona
Nov 16 '18 at 18:27
add a comment |
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%2f53342797%2fadding-object-into-a-list-with-function%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
1
It's unclear what you want exactly. Provide some input and expected output.
– SilverSlash
Nov 16 '18 at 17:38
I updated the question
– Void Beats
Nov 16 '18 at 18:06
>>I want to have a program that will create an object when you call a function, and that will automatically append it into a list.<< Correct me if I am wrong but you want to have an object that has a method that will create this object itself?
– artona
Nov 16 '18 at 19:08