Question

In python, why is it advantageous to store not just data (as vectors, arrays,etc.) but perhaps...

In python, why is it advantageous to store not just data (as vectors, arrays,etc.) but perhaps the object (an instance of your user-defined class) that contains and isable to manipulate these data in a data file through maybe the pickle module? Withoutimporting your own class definition, will the data loaded from such a data file be usableor interpretable? please also explain with code its so URGENT

Homework Answers

Answer #1

solution:

given data:

By storing the object intance, you are able to store the complete current state of the object along with the data. So when you need to re-create the object, you can easily do without manual data parsing.

You need to import the class definition, in order to manipulate data. Otherwise, data manipulation on the object will not work, because pickle only stores the state of the object, not the class definition.

The two python files demo this process. On file has the class definition saves an object to a file. The other file mporta the class defintion and it reads the data from the file, and invokes a function of the object.

Program Screenshots for Indentation Reference:

Sample Output:

Program code to copy:

picklerDemo.py:

from pickle import Pickler


# custom class for demo
class Person:
    def __init__(self, name):
        self.name = name
  
    def printName(self):
        """ Function to print the name"""
        print(self.name)


#create an object and save object to file
obj = Person("John Doe")

with open("john.pickle", "wb") as f:
    Pickler(f).dump(obj)

unpickleDemo.py:

from pickleSaver import Person
from pickle import Unpickler


with open("john.pickle", "rb") as f:
    obj = Unpickler(f).load()

    # invoke method of the object
    obj.printName()

please give me thumb up

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
**[70 pts]** You will be writing a (rather primitive) online store simulator. It will have these...
**[70 pts]** You will be writing a (rather primitive) online store simulator. It will have these classes: Product, Customer, and Store. All data members of each class should be marked as **private** (a leading underscore in the name). Since they're private, if you need to access them from outside the class, you should do so via get or set methods. Any get or set methods should be named per the usual convention ("get_" or "set_" followed by the name of...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields...
Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number...
Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number of servings that are really possible with the included ingredients. Sometimes one will want to be able to scale those recipes upwards for serving larger groups. This program's task is to determine how much of each ingredient in a recipe will be required for a target party size. The first inputs to the program will be the recipe itself. Here is an example recipe...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT