Programming Python, Part I
Having to create an object and then set each of its members is not pleasant. It takes a lot of lines and is very error-prone—did I remember to set the tags? There's a better way to do it—using the initialization method.
This special method is called __init__, and the parameters you define it to take have to be passed in the creation of the object. A possible initialization method would be:
class Post(object): def __init__(self, title, body): self.set_title(title) self.set_body(body)
Simply add the __init__ definition to the file and reload it. We now can, and have to, set the title and body at initialization time:
>>> cool = blog.Post("Cool", "Python is cool") >>> cool.get_title() 'Cool' >>> cool.get_body() 'Python is cool' >>>
Hint: to retrieve previous lines in the REPL inside Emacs use Alt-P.
There are other special methods. Remember how ugly it was to evaluate a Post itself? Let me remind you:
>>> cool <blog.Post object at 0xb7c7e9ac>
We can solve that. There's another special method called __repr__, which is used to retrieve that string. Inside the Post class add:
def __repr__(self): return "Blog Post: %s" % self.get_title()
Reload the file, the same way you loaded it previously, and evaluate a post:
>>> ## working on region in file /usr/tmp/python... >>> cool <blog.Post object at 0xb7c7e9ac> >>>
Oops! That's not what we wanted. The problem here is that the cool object was created with an older version of the Post class, so it doesn't have the new method. That is a very common mistake, and not being prepared for it can cause a lot of headaches. But, simply re-create the object, and you are set:
>>> ## working on region in file /usr/tmp/python... >>> cool = blog.Post("Cool", "Python is cool") >>> cool Blog Post: Cool >>>
Easy—wait for the next issue of Linux Journal for Part II of this tutorial. If you really want something to do now, start learning Emacs.
José P. E. “Pupeno” Fern´ndez has been programming since...at what age is a child capable of siting in a chair and reaching a keyboard? He has experimented with more languages than can be listed on this page. His Web site is at pupeno.com, and he always can be reached, unless you are a spammer, at firstname.lastname@example.org.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Fancy Tricks for Changing Numeric Base
- Secure Desktops with Qubes: Introduction
- Seeing Red and Getting Sleep
- Working with Command Arguments
- Secure Desktops with Qubes: Installation
- CentOS 6.8 Released
- The Italian Army Switches to LibreOffice
- Linux Mint 18
- Petros Koutoupis' RapidDisk
Until recently, IBM’s Power Platform was looked upon as being the system that hosted IBM’s flavor of UNIX and proprietary operating system called IBM i. These servers often are found in medium-size businesses running ERP, CRM and financials for on-premise customers. By enabling the Power platform to run the Linux OS, IBM now has positioned Power to be the platform of choice for those already running Linux that are facing scalability issues, especially customers looking at analytics, big data or cloud computing.
￼Running Linux on IBM’s Power hardware offers some obvious benefits, including improved processing speed and memory bandwidth, inherent security, and simpler deployment and management. But if you look beyond the impressive architecture, you’ll also find an open ecosystem that has given rise to a strong, innovative community, as well as an inventory of system and network management applications that really help leverage the benefits offered by running Linux on Power.Get the Guide