Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Python Design Patterns - Second Edition

You're reading from   Learning Python Design Patterns - Second Edition

Arrow left icon
Product type Paperback
Published in Feb 2016
Publisher
ISBN-13 9781785888038
Length 164 pages
Edition 2nd Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Gennadiy Zlobin Gennadiy Zlobin
Author Profile Icon Gennadiy Zlobin
Gennadiy Zlobin
Chetan Giridhar Chetan Giridhar
Author Profile Icon Chetan Giridhar
Chetan Giridhar
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Introduction to Design Patterns 2. The Singleton Design Pattern FREE CHAPTER 3. The Factory Pattern – Building Factories to Create Objects 4. The Façade Pattern – Being Adaptive with Façade 5. The Proxy Pattern – Controlling Object Access 6. The Observer Pattern – Keeping Objects in the Know 7. The Command Pattern – Encapsulating Invocation 8. The Template Method Pattern – Encapsulating Algorithm 9. Model-View-Controller – Compound Patterns 10. The State Design Pattern 11. AntiPatterns Index

Understanding object-oriented programming

Before you start learning about design patterns, it's always good to cover the basics and go through object-oriented paradigms in Python. The object-oriented world presents the concept of objects that have attributes (data members) and procedures (member functions). These functions are responsible for manipulating the attributes. For instance, take an example of the Car object. The Car object will have attributes such as fuel level, isSedan, speed, and steering wheel and coordinates, and the methods would be accelerate() to increase the speed and takeLeft() to make the car turn left. Python has been an object-oriented language since it was first released. As they say, everything in Python is an object. Each class instance or variable has its own memory address or identity. Objects, which are instances of classes, interact among each other to serve the purpose of an application under development. Understanding the core concepts of object-oriented programming involves understanding the concepts of objects, classes, and methods.

Objects

The following points describe objects:

  • They represent entities in your application under development.
  • Entities interact among themselves to solve real-world problems.
  • For example, Person is an entity and Car is an entity. Person drives Car to move from one location to the other.

Classes

Classes help developers to represent real-world entities:

  • Classes define objects in attributes and behaviors. Attributes are data members and behaviors are manifested by the member functions
  • Classes consist of constructors that provide the initial state for these objects
  • Classes are like templates and hence can be easily reused

For example, class Person will have attributes name and age and member function gotoOffice() that defines his behavior for travelling to office for work.

Methods

The following points talk about what methods do in the object-oriented world:

  • They represent the behavior of the object
  • Methods work on attributes and also implement the desired functionality

A good example of a class and object created in Python v3.5 is given here:

class Person(object):
    def __init__(self, name, age):  #constructor
        self.name = name    #data members/ attributes
        self.age = age
    def get_person(self,):   # member function
         return "<Person (%s, %s)>" % (self.name, self.age)


p = Person("John", 32)    # p is an object of type Person
print("Type of Object:", type(p), "Memory Address:", id(p))

The output of the preceding code should look as follows:

Methods
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image