user1862963
user1862963

Reputation: 79

object destruction in python

I am relatively new to python. Let me put my doubt in an example:

I create my class object called "person"

class person:
    name = ""
    age = ""
    phones = []
    def add_phone(self, number):
        self.phones.append(number)

let's prepare some data to be used later on:

names = ["jhon", "tony", "mike", "peter"]
ages = [34, 36, 23, 75]
phones = [7676, 7677, 7678, 7679]

Let's enter a for loop:

for i in range(0,4):
    new_person = person()

To my understanding, every time the preceding line is executed a new object person is created and called new_person, and any object in the variable new_person from a previous iteration should be destroyed. However this is not the case:

    new_person.name = names[i]
    new_person.age = ages[i]
    new_person.add_phone(phones[i])

    print "i= " + str(i)
    print "name= "+ new_person.name
    print "age= "+ str(new_person.age)
    print "phones:"
    print new_person.phones

new_person.phones contains the phones that we have added in previous iterations. Does anybody see what I am missing? Does this mean that the object class "person" behaves like a singleton?

Upvotes: 1

Views: 1286

Answers (3)

Oleksii Kachaiev
Oleksii Kachaiev

Reputation: 6234

To work as expected, you need to define phones as instance attribute.

class person:
    name = ""
    age = ""
    def __init__(self):
        self.phones = []
    def add_phone(self, number):
        self.phones.append(number)

In your example, you append phone to class attribute, which is "shared" for all instances (as to say).

Upvotes: -1

NPE
NPE

Reputation: 500407

The objects are getting created and destroyed as you'd expect. The problem is that self.phones belongs to the class rather than the instance and is therefore shared by all instances of person.

To fix, change your class like so:

class person(object):
    def __init__(self):
        self.name = ""
        self.age = ""
        self.phones = []
    def add_phone(self, number):
        self.phones.append(number)

Upvotes: 3

Larry Lustig
Larry Lustig

Reputation: 50980

You're defining a bunch of class-level attributes that should almost certainly be instance level. I think what you want is:

class person:

    def __init__(self):

        self.name = ""
        self.age = ""
        self.phones = []

    def add_phone(self, number):

        self.phones.append(number)

The way instantiation is handled in Python is that the initializer __init__() (similar to but not the same as a constructor in other languages) creates instance attributes. In Python it's probably more common to set uninitialized attributes to None rather than empty strings. Also, it's common to include frequently-used attributes in the initializer:

    def __init__(self, name = None, age = None, phones = None):

        self.name = name
        self.age = age
        if phones = None:
            self.phones = []
        else:
            self.phones = phones

Upvotes: 2

Related Questions