0

I am learning python at the moment the task is this; Write a program that reads pet information (name, type and age) from a file (called animals.txt) and creates Pet objects (using the information stored in the animals.txt file). Store Pet objects in a list called animals.

animal.txt

ralph, dog, 3
buster, cat, 8
sammy, bird, 5
mac, dog, 1
coco, cat, 6

My class file i created is called pet.py

class Pet:
    # The __init__ method initializes the data attributes of the Profile class
    def __init__(self, name ='', animal_type = '', age = ''):
        self.__name = name
        self.__animal_type = animal_type
        self.age = 0

    def __str__(self):
        string = self.__name + ' ' + self.__animal_type + ' ' + self.age
        return string

    def set_name(self, name):
        self.__name = name

    def get_name(self):
        return self.__name

    def set_animal_type(self, breed):
        self.__animal_type = breed

    def get_animal_type(self):
        return self.__animal_type

    def set_age(self, old):
        self.age = old    

    def get_age(self):
        return self.age

I then want to use this class in a file animals.py

import pet

animals = [] // create a list 

infile = open("animals.txt", "r") // open the file

lines = infile.readlines() // read all lines into list

## add each pet object
for line in lines:
    data = line.split(",")
    animals.append(pet.set_name(data[0]))
    animals.append(pet.set_animal_type(data[1]))
    animals.append(pet.set_age(data[2]))

infile.close()

I am getting an error

pet.set_name [pylint] E1101 : module 'pet' has no 'set_name' member.

If i do this code below in the class file pet.py I don't get the error

pet = Pet()
name = "thing"
breed = "dog"
pet.set_name(name)
pet.set_animal_type(breed)
pet.set_age(10)
print(pet)

and it returns as expected

thing dog 10

Why wont the animals.py file allow me to use the class i have imported?

I have tried pet=Pet() but it has

error E0602: undefined variable 'Pet'

1
  • from pet import Pet then use Pet as you did before. Commented Oct 20, 2018 at 2:30

2 Answers 2

1

In your animals.py file pet represents a module. You need to extract the class located within that module like this:

import pet

myPet = pet.Pet()
Sign up to request clarification or add additional context in comments.

1 Comment

TY .. this did work and i see what i did wrong there :-) I am now meant to append this data to the animal list .. I thought i would do this with --> animals.append(myPet) although the output with print(animals) is --> <pet.Pet object at 0x000001B110E89B00> repeated 5 times .. when I use print(myPet) with each iteration the details are correct but not able to append the same object to the list.
1

Right now you're importing the entire pet module's contents. You can gain access to the Pet class in one of two ways.

The first requires you to use the object's entire dotted path

import pet

pet.Pet(...)

The second requires you to import the Pet class

from pet import Pet

Pet(...)

One gotcha here is that depending on your folder structure, Python may not be able to identify your file as importable, so you may need to create a blank file named __init__.py at the same position in your directory structure as pet.py.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.