Book Image

Dancing with Python

By : Robert S. Sutor
Book Image

Dancing with Python

By: Robert S. Sutor

Overview of this book

Dancing with Python helps you learn Python and quantum computing in a practical way. It will help you explore how to work with numbers, strings, collections, iterators, and files. The book goes beyond functions and classes and teaches you to use Python and Qiskit to create gates and circuits for classical and quantum computing. Learn how quantum extends traditional techniques using the Grover Search Algorithm and the code that implements it. Dive into some advanced and widely used applications of Python and revisit strings with more sophisticated tools, such as regular expressions and basic natural language processing (NLP). The final chapters introduce you to data analysis, visualizations, and supervised and unsupervised machine learning. By the end of the book, you will be proficient in programming the latest and most powerful quantum computers, the Pythonic way.
Table of Contents (29 chapters)
Part I: Getting to Know Python
PART II: Algorithms and Circuits
PART III: Advanced Features and Libraries
Other Books You May Enjoy
Appendix C: The Complete UniPoly Class
Appendix D: The Complete Guitar Class Hierarchy
Appendix F: Production Notes

7.12 Class and static methods

Class methods and static methods are both defined within a class. Unlike an instance method, Python does not bind a class method to a particular instance. It is bound to and knows about the entire class. A static method is a function that sits within a class but knows nothing special about the class or its instances.

You create a class method by placing the @classmethod decorator on the line before the method’s definition. Instead of using self for the first parameter as you would in an instance method, use “cls”. At runtime, Python passes the class object to the method as the first argument.

def get_count_class(cls):
    return cls.number_of_guitars

If you refer to a class variable within a class method, prefix the variable name with cls and a “.”. Do not hardcode the name of the class into the call, as...