Skip to Content
Secret Recipes of the Python Ninja
book

Secret Recipes of the Python Ninja

by Cody Jackson
May 2018
Intermediate to advanced content levelIntermediate to advanced
380 pages
9h 37m
English
Packt Publishing
Content preview from Secret Recipes of the Python Ninja

How to do it...

  1. Class methods can be decorated as well. Instance methods are the most common form of methods, that is, functions in classes. Here is cat_class.py with a few methods to work with:
        class Cat():            def __init__(self, breed, age):                """Initialization method to auto-populate an instance"""                self.breed = breed                self.age = age            def cat_age(self):                """Get the cat's age"""                return self.age            def breed(self):                """Get the type of cat, e.g. short hair, long hair, etc."""                        return self.breed            def __repr__(self):                """Return string representation of Cat object.                Without this method, only the object's                 memory address will be printed.                """                return "{breed}, {age}".format(breed = self.breed, age = self.age)
  1. To utilize this class, create an instance of Cat ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

The Expanding World of Python

The Expanding World of Python

Dane Hillard

Publisher Resources

ISBN: 9781788294874Supplemental Content