beanz Magazine

Objects

corndog_au on Flickr

Many programming languages organize their code into objects, to model their applications and make their code more reusable.

This Code Snippets section of the magazine explains parts of programming languages common across most or all languages. It's a great way to understand how languages work. Learning how to code should be much easier and less frightening if I explain a part of a programming language, then show you how it works in a couple different languages.

Let's get started by talking about how objects are used in Python and PHP.

Object oriented programming is a way to organize software programs. It's a collection of data and functions that do things. It's a way to design applications carefully with an eye to reuse of code. Many languages support objects and object oriented programming.

A class is a template used to create an object. Each object has properties and methods. For example, if you have an object called Dog, you might have properties called a tail or legs. Methods might be eat, drink, walk, sit, and beg. Methods are actions your object can perform. Methods also make sense: it’s unlikely the Dog object would have a method called drive because dogs don’t drive, despite the photo attached above this article.

Since furry pets are fun, we'll talk at a high level about objects used in programming by talking about dogs.

Usually objects are referred to in dot syntax, the use of dots (or periods) between different words that, together, tell the software what to do. Here's an example:

dog.tail.wag

Used as code, this might get your dog to wag its tail. dog is the object, tail is a property. wag is a method or action one or more properties in the dog object can perform. If this pretend bit of code worked, calling dog.tail.wag would cause the dog to wag its tail.

With this as a high level, and non-serious, introduction to the basics of objects and object oriented programming, let's look at how two languages create objects, properties, and methods.

Python

Here's a simple class in Python we can use to create a Dog object:

#dog.py
class Dog:
   " " " Class to represent a dog
   " " "
   def __init__(self)
      self.name = ''
      self.age = 0

At the Python prompt in a command line software tool like Terminal, you would type these lines:

>>>d = Dog()
>>>d.name
''
>>>d.age
0

In this case, typing d.age returns the initial value we defined in line 7 above when we coded a Python class to create the Dog object. Same result for d.name, on line 6 above, which is blank because we did not define a default name for our dog.

Notice the first step is to assign all the details about the Dog object to the variable d: d = Dog(). Then we use the d variable as a prefix at the start of the code to call a property inside our Dog object. This may look like magic but it’s standard in coding to assign one thing to another. You get used to it.

We also can assign values to our name and age properties, again at the Python prompt in our command line software:

>>>d.age = 5
>>>d.age
5

There's a lot more we could do with our Dog object but this provides a high level overview how to define, set, and retrieve properties and methods from a Python object.

PHP

PHP uses objects and object-oriented programming in a similar way to Python but with the typical PHP syntax of curly braces { } to mark off the start and end of functions and statements:

<?php
class Dog
{
    //declare properties
    public $name = "Bowser";
    public $age = 0;

    //declare methods
    public function getName() {
        echo $this->name;
    }
    public function printName($string) {
        echo "Your dog&#39;s name: " . $string;
    }
?>

This PHP code will display the name Bowser when called in a PHP script:

$Dog->getName();

While this code will display the name Daisy, or whatever name you put in quotes:

$Dog->printName("Daisy");

Notice how, unlike Python, we create a variable called $Dog and use it to assign the properties and methods of the Dog object class. The syntax is $Dog-> where the -> indicates we’re assigning everything in the Dog object to this variable then, to the right of the -> characters, we specify what to do and what to method to call.

Also notice on lines 9 and 12 above in our PHP code, we define the getName() and printName() functions as public. Methods in PHP can be public, private, or protected. These are all ways to define the scope, specifically, who can see and use these methods. Private methods, for example, are available only within the object. Perhaps one function in the Dog object needs to call another.

The scope of methods is another aspect of objects common across many programming languages. These examples only demonstrate the basics of how objects, properties, and methods are created in Python and PHP, as well as simple ways to retrieve and update values of properties.

Learn More

Object Oriented Programming

http://en.wikipedia.org/wiki/Object-oriented_programming
http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3/Intro_to_Object_Oriented_Programming_in_Python_3

Objects in Python

http://docs.python.org/2/tutorial/classes.html
http://www.packtpub.com/article/python-when-to-use-object-oriented-programming
http://www.tutorialspoint.com/python/python_classes_objects.htm
http://docs.python-guide.org/en/latest/writing/structure/

Objects in PHP

http://www.php.net/manual/en/language.oop5.php
http://www.php.net/manual/en/language.types.object.php
http://www.tutorialspoint.com/php/php_object_oriented.htm

Pros and Cons of Object Oriented Programming (OOP)

http://java.dzone.com/articles/simple-guide-pros-and-cons
http://programmers.stackexchange.com/questions/9730/functional-programming-vs-oop
http://c2.com/cgi/wiki?ArgumentsAgainstOop

Also In The April 2014 Issue

How to Make a Twitter Bot

There are a ton of ways to make a Twitter bot. Here are a number of resources, ideas, and clues to follow up.

What is an Algorithm?

Algorithms control our lives online and offline. Here are some examples to show what is an algorithm and how they work.

How to Build Example Bot

I have updated the installation instructions for this Github project to make it even easier to install and play with.

Bot Makers

A collection of bot makers and professor types attend an online bot convention to talk about Twitter bots and more.

File Extensions

You can tell a bit about the software used to create the web page you're reading by looking at the URL in your web browser.

Traceroute

A computer command used to diagnose network problems also lets you see how data hops across the internet.

How to Build a Slightly Complicated Website

Some tips on how to find online publishing services and use them to create your website.

What is an API?

APIs can be a fun, easy to use, and easy to learn way to retrieve and mash up data online.

April 2014 News Wire

Interesting stories about computer science, software programming, and technology for the month of March 2014.

Objects

Many programming languages organize their code into objects, to model their applications and make their code more reusable.

April 2014 Learn More Links

Links from the bottom of all the April 2014 articles, collected in one place for you to print, share, or bookmark.

At bottom, robotics is about us. It is the discipline of emulating our lives, of wondering how we work.