Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Learn Python in 7 Days

You're reading from   Learn Python in 7 Days Begin your journey with Python

Arrow left icon
Product type Paperback
Published in May 2017
Publisher Packt
ISBN-13 9781787288386
Length 280 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
 Das Das
Author Profile Icon Das
Das
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Title Page
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
1. Getting Started with Python FREE CHAPTER 2. Type Variables and Operators 3. Strings 4. Lists 5. Dictionary 6. Control Statements and Loops 7. Function and Scope of Variable 8. Modules and Packages 9. File Handling and Exceptions 10. Collections 11. Class and Objects

Instance variables


Instance variables refer to the data that are unique to instances (objects). Let's create an instance variable. We will write a new class2.py program:

class Leapx_org():
  pass
L_obj1 = Leapx_org()
L_obj2 = Leapx_org() 
L_obj1.f_name = "Mohit"
L_obj1.L_name = "RAJ"
L_obj1.pamount = "60000"
L_obj2.f_name = "Ravender"
L_obj2.L_name = "Dahiya"
L_obj2.pamount = "70000"
print L_obj1.f_name+ " "+ L_obj1.L_name
print L_obj2.f_name+ " "+ L_obj2.L_name

 

In the preceding code, L_obj1.f_name, L_obj1.L_name, and L_obj1.pamount are the instance variables, which are unique to the L_obj1 instance. Similarly, L_obj2.f_name, L_obj2.L_name, and L_obj2.pamount are the instance variables of the L_obj2 instance.

Let's run the code:

Output of code class2.py

If we create an instance variable, as shown in the code, then we would not get benefit of making a class. You can see repeatable code for both the instances. So we would not have to set the variable all the time. In order to make it automatically...

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 $15.99/month. Cancel anytime
Visually different images