Source

pyptug / islanders / presentation / slides.md

Full commit

Hands on Python: From Java to Burma

by Francois Dion @f_dion

PYPTUG

PYPTUG @WFU Monday August 26th 2013


Hands on Python: From Java to Burma

Java

The Islanders

Getters and setters are an engrained pattern for the islanders of Java, C++ and some other islanders. Naturally, as these islanders travel to Myanmar and encounter The Python (well, yes, we do know that Python is from that TV show, and not the reptile, be it a Burmese or other kind), they continue these ancient customs.

In this hands on session, we will show you how to keep it simple and we will get deep into attributes, properties, descriptors and have fun with them.


Meet Benji

Benji

Attributes?


Attributes

  • age
  • gender
  • race
  • color
  • height
  • weight
  • vaccinated
  • hair
  • bark

Namespaces

Just like math

age=1

My age and Benji's age

print(age)

How is the computer supposed to know if it is my age, your age or Benji's age.

We need a container to hold all the attributes for benji, and a completely different one for you and I.

Puppy, Human

!python
Puppy.age
Human.age

We can do it with modules or with classes, but...


Modules

!python
import Puppy

hands on


Class

!python
class Puppy(object):

hands on

What if I wanted to add dog years?


Java

!java
public class Puppy{

    private int age;

    public Puppy(){
    }   
    public void setAge( int theAge ){
        age = theAge;
    }
    public int getAge( ){
        return age;
    }

    public static void main(String []args){
        Puppy benji = new Puppy();

        benji.setAge( 2 );

        System.out.println("age:" + benji.getAge( )); 
    }
}

C++

!c++
#include <iostream>
using namespace std;

class Puppy{
    int m_Age;

    public: 
    int  getAge() const { 
        return m_Age; 
    }
    void setAge(int age) {
        m_Age = age;
    }
};

int main( int argc, const char* argv[] )
{
Puppy benji;

benji.setAge(1);
cout << "age:" << benji.getAge() << endl;
}

C# seen in the wild

!c#
using System;

public class Puppy
{
    private int puppyAge;
    public int GetAge()
    {
        return puppyAge;
    }   
    public void SetAge(int value)
    {
        puppyAge = value;
    }
    static public void Main ()
    {
        Puppy benji = new Puppy ();

        benji.SetAge(1);
        Console.WriteLine ("age: " + benji.GetAge());
    }
}

C# with properties

!c#
public class Puppy
{
    private string puppyAge;

    public string age
    {
        get { return puppyAge; }
        set { puppyAge = value; }
    }
    static public void Main ()
    {
        Puppy benji = new Puppy ();

        benji.SetAge(1);
        Console.WriteLine ("age: " + benji.GetAge());
    }

}

Always start with attributes

Unless you have a good reason not to.

You can always come back and change to properties later on.

hands on

(next page is definition of property class)


Properties

!python
class property(object)
|  property(fget=None, fset=None, fdel=None, doc=None) -> property
|  attribute
|  
|  fget is a function to be used for getting an attribute value, and
|  fset is a function for setting, and fdel a function for del'ing,
|  an attribute.  Typical use is to define a managed attribute x:
|  class C(object):
|      def getx(self): return self._x
|      def setx(self, value): self._x = value
|      def delx(self): del self._x
|      x = property(getx, setx, delx, "I'm the 'x' property.")

hands on


Decorators

!python
|  Decorators make defining new properties or modifying existing ones
|  easy:
|  class C(object):
|      @property
|      def x(self): return self._x
|      @x.setter
|      def x(self, value): self._x = value
|      @x.deleter
|      def x(self): del self._x

hands on


Descriptors

Going deep now, for those that are starting to get overwhelmed, as an exercise, try to add other attributes to your Puppy class. Ignore my talking. For the others, hold on to your hats.

Defined

In general, a descriptor is an object attribute with “binding behavior”, one whose attribute access has been overridden by methods in the descriptor protocol.

Say what?

For our intent and purpose, we are talking about the main use of descriptors in daily coding: custom descriptors

  • can allow reuse of properties
  • specialized behaviour when accessed, set or deleted

In fact, we do that now with property, which is a descriptor, and so would be staticmethod and classmethod.


Descriptors - continued

accessed, set or deleted

Those methods are __get__(), __set__(), and __delete__(). If any of those methods are defined for an object, it is said to be a descriptor.

Let's do it

hands on


Hands on Python: From Java to Burma

by Francois Dion @f_dion

PYPTUG

PYPTUG @WFU Monday August 26th 2013