Book Image

Learn Python in 7 Days

Book Image

Learn Python in 7 Days

Overview of this book

Python is a great language to get started in the world of programming and application development. This book will help you to take your skills to the next level having a good knowledge of the fundamentals of Python. We begin with the absolute foundation, covering the basic syntax, type variables and operators. We'll then move on to concepts like statements, arrays, operators, string processing and I/O handling. You’ll be able to learn how to operate tuples and understand the functions and methods of lists. We’ll help you develop a deep understanding of list and tuples and learn python dictionary. As you progress through the book, you’ll learn about function parameters and how to use control statements with the loop. You’ll further learn how to create modules and packages, storing of data as well as handling errors. We later dive into advanced level concepts such as Python collections and how to use class, methods, objects in python. By the end of this book, you will be able to take your skills to the next level having a good knowledge of the fundamentals of Python.
Table of Contents (18 chapters)
Title Page
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
2
Type Variables and Operators

The class method


You have seen the regular class methods of the class. The regular method automatically takes an instance as the first argument, and, by convention, we called it self. How can we pass the class as an argument so that we can change the class variable in the method? To do that, we use the class method. The class method takes the class as first argument. To turn the regular method into the class method, we will use decorator (@classmethod) at the top of the method. Let's see the methodclass1.py example:

class Leapx_org():
    mul_num = 1.20
    def __init__(self,first,last,pay):
       self.f_name = first
       self.l_name = last
       self.pay_amt = pay 
       self.full_name = first+" "+last

    def make_email(self):
       return self.f_name+ "."+self.l_name+"@xyz.com"

    def incrementpay(self):
       self.pay_amt = int(self.pay_amt*self.mul_num)
       return self.pay_amt

    @classmethod
    def mul_num_set(cls, amt):
       cls.mul_num=amt

L_obj1 = Leapx_org('mohit...