Выбрать главу

mycar = modelt()

mycar.setColor("green")

The first car is created as a Ford, so setColor() works fine because it uses the method from the car class. However, the second car is created as a Model T, which has its own setColor() method, so the call will fail.

This provides an interesting scenario: What do you do if you have overridden a method and yet want to call the parent's method also? If, for example, changing the color of a Model T was allowed but just cost extra, you would want to print a message saying, "You owe $50 more," but then change the color. To do this, you need to use the class object from which the current class is inherited — car, in this example. Here's an example:

class modelt(car):

 def setColor(self, color):

  print "You owe $50 more"

car.setColor(self, color)

mycar = modelt()

mycar.setColor("green")

print mycar.color

That prints the message and then changes the color of the car.

Multiple Inheritance

You can inherit as many classes as you need, building up functionality as you go. For example, you could have a class animalia, a subclass chordata, a sub-subclass mammalia, and a sub-sub-subclass homosapiens. Each one is more specific than its parent. However, an interesting addition in Python is the capability to have multiple inheritance — to take functionality from two classes simultaneously.

Again, this is best shown in code:

class car(object):

 def drive(self):

  print "We're driving..."

class timemachine(object):

 def timeTravel(self):

  print "Traveling through time..."

class delorian(car,timemachine): pass

mydelorian = delorian()

mydelorian.drive()

mydelorian.timeTravel()

In that example, you can see a class car and a class timemachine. Both work by themselves, so you can have a car and drive around in it or a time machine and travel through time with it. However, there is also a delorian class that inherits from car and timemachine. As you can see, it is able to call both drive() (inherited from car) and timeTravel() (inherited from timemachine).

This introduces another interesting problem: What happens if both car and timemachine have a refuel() function? The answer is that Python picks the correct function to use based on the order in which you listed the parent classes. The previous code used class delorian(car,timemachine), which means "inherit from car and then from timemachine." As a result, if both classes had a refuel() function, Python would pick car.refuel().

This situation becomes more complex when further inheritance is involved. That is, if car inherits its refuel() method from vehicle, Python still chooses it. What happens behind the scenes is that Python picks the first class from which you inherited and searches it and all its parent classes for a matching method call. If it finds none, it goes to the next class and checks it and its parents. This process repeats until it finds a class that has the required method.

The Standard Library and the Vaults of Parnassus

A default Python install includes many modules (blocks of code) that enable you to inter act with the operating system, open and manipulate files, parse command-line options, perform data hashing and encryption, and much more. This is one of the reasons most commonly cited when people are asked why they like Python so much—it comes stocked to the gills with functionality you can take advantage of immediately. In fact, the number of modules included in the Standard Library is so high that entire books have been written about them — try Python Standard Library (O'Reilly, ISBN: 0-596-00096-0) for a comprehensive, if slightly dated, list of them.

For unofficial scripts and add-ons for Python, the recommended starting place is called the Vaults of Parnassus: http://py.vaults.ca/. There you can find about 20,000 public scripts and code examples for everything from mathematics to games.

Reference

► http://www.python.org/ — The Python website is packed with information and updated regularly. This should be your first stop, if only to read the latest Python news.

► http://www.zope.org/ — The home page of the Zope Content Management System (CMS), it's one of the most popular CMSes around and, more importantly, written entirely in Python.

► http://www.jython.org/ — Python also has an excellent Java-based interpreter to allow it to run on other platforms. If you prefer Microsoft's .NET, try http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython.

► http://www.pythonline.com/ — Guido van Rossum borrowed the name for his language from Monty Python's Flying Circus, and as a result, many Python code examples use oblique Monty Python references. A visit to the official Monty Python site to hone your Python knowledge is highly recommended!

► http://www.python.org/moin/PythonBooks — There are few truly great books about Python; however, you can find a list of what's on offer at this site. If you are desperate to pick up a book immediately, you could do much worse than to choose Learning Python (O'Reilly, ISBN: 0-596-00281-5).

CHAPTER 27

Writing PHP Scripts

This chapter introduces you to the world of PHP programming, from the point of view of using it as a web scripting language and as a command-line tool. PHP originally stood for personal home page because it was a collection of Perl scripts designed to ease the creation of guest books, message boards, and other interactive scripts commonly found on home pages. However, since those early days, it has received two major updates (PHP 3 and PHP 4), plus a substantial revision in PHP 5, which is the version bundled with Fedora.

Part of the success of PHP has been its powerful integration with databases — its earliest uses nearly always took advantage of a database back end. In PHP 5, however, two big new data storage mechanisms were introduced: SQLite, which is a powerful and local database system, and SimpleXML, which is an API designed to make XML parsing and querying easy. As you will see over time, the PHP developers did a great job because both SQLite and SimpleXML are easy to learn and use.

NOTE

PHP's installation packages are under the Web Server category in Add/Remove Applications. The basic package is just called php, but you might also want to add extensions such as php_ldap, php_mysql, or php_pgsql. Choose only the extensions you plan to use; otherwise, you will waste system resources.

Introduction to PHP

In terms of the way it looks, PHP is a cross between Java and Perl, having taken the best aspects of both and merged them successfully into one language. The Java parts include a powerful object-orientation system, the capability to throw program exceptions, and the general style of writing that both languages borrowed from C. Borrowed from Perl is the "it should just work" mentality where ease of use is favored over strictness. As a result, you will find a lot of "there is more than one way to do it" in PHP.