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;
}







0















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.










share|improve this question




















  • 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


















0















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.










share|improve this question




















  • 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














0












0








0








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












2 Answers
2






active

oldest

votes


















0














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']





share|improve this answer































    0














    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





    share|improve this answer


























    • 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














    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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']





    share|improve this answer




























      0














      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']





      share|improve this answer


























        0












        0








        0







        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']





        share|improve this answer













        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']






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 17:45









        AustinAustin

        13.2k31031




        13.2k31031

























            0














            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





            share|improve this answer


























            • 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


















            0














            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





            share|improve this answer


























            • 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
















            0












            0








            0







            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





            share|improve this answer















            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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            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





















            • 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




















            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            The Sandy Post

            Danny Elfman

            Pages that link to "Head v. Amoskeag Manufacturing Co."