9/1/2023 0 Comments SerializationWe mentioned almost everything in Python is an object. By the way, if you’re not familiar with using the with statement to save a file, here’s some material about writing to file in Python. The two objects fast_car1 and fast_car2 are two distinct objects with different locations in memory however, they have the same attributes and methods. Note the different modes we use for serializing (' wb') and deserializing (' rb'). With open('fast_car_object.pkl', 'rb') as in_file: To unpickle this file, simply do the following: With open('fast_car_object.pkl', 'wb') as out_file:Įxecuting this code produces the file fast_car_object.pkl in your current working directory. With the safety briefing over, let’s continue with an example of pickling and unpickling in Python: Therefore, never unpickle data received from an untrusted source. But before we do, a word of caution: as stated in the official documentation, the Python pickle module is not secure against maliciously constructed data that can execute foreign code. We’ll take a closer look at the first two below. The two other methods are dumps() and loads(), which serialize objects without saving them to file. The two we’ll use for our case are dump() and load(), which saves and loads pickle files, respectively. The Python pickle module stores the data in a binary form, so it isn’t human-readable. Functions and classes can be serialized, and as we will see below, so can instances of classes. A large number of objects can be pickled, including Booleans, integers, floats, and strings, as well as data structures such as lists, dictionaries, sets, etc. The Python pickle module is an easy-to-use module for serializing (pickling) and deserializing (unpickling) objects in Python. Now we would like to serialize the fast_car1 object so we can come back and use it later without having to instantiate it again. Let’s take it for a drive for 8 km, then check the fuel level: We can create a car object with an efficiency of 5 km/L and a fuel level of 12 L as follows: Self.fuel_level -= distance / self.efficiency Print('Traveled %s km, out of fuel'%(max_distance)) Max_distance = self.fuel_level * self.efficiency The following class allows you to drive a car a certain distance given its fuel level and efficiency:ĭef _init_(self, efficiency, fuel_level): These are the class methods, which are written as functions within the class. Cars can also have several actions associated with them, such as driving a certain distance. The important attributes for our car class are the current fuel level and the efficiency (how much fuel it consumes per kilometer). We also have some material that builds on top of that and shows you how to write a custom module in Python.Īn object in Python has attributes (like its properties) and methods (things it can do). The focus of this article isn’t on classes, so if you want more details on how classes work, take a look at this article. Let’s get into it and create a class for driving a car. This means you can serialize data stored in a list, a dictionary, or even instances of classes. Remember that Python is an object-oriented language almost everything is considered an object.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |