Sunday, June 14, 2015

Creating and Accessing Attributes

The constructor method in this program has the attribute of name. The __str__() method creates a string representation of the object. See different ways to access attributes in the program below.



Constructor Method

A constructor is a method that is invoked automatically when a new object is created.

Creating Class, Method, and Objects

This simple program creates a class (virtual_pet), object (pet1), method (talk).

Friday, April 24, 2015

Pickling and Shelving Data

The following program stores data to a .dat file rather than a .txt file. To store more complex information, such as lists, .dat files are used. The shelf module allows you to randomly access data. It basically takes pickled data one step further by making a dictionary out of the lists which can be randomly accessed.



Thursday, April 23, 2015

Writing to a text file

The following program write three lines and creates a text file. The writelines method can be used to write lines from a list to a text file.