PYTHON PROGRAMMING 1
PYTHON PROGRAMMING 1
SELF.NAME = NAME
SELF.AGE = AGE
SELF.GENDER = GENDER
DEF __STR__(SELF):
PRINT(PERSON1)
PRINT(PERSON2)
EXAMPLE 2.
IN THE FOLLOWING EXAMPLE, IF WE OMIT MENTIONING THE METHOD, THEN THE
DESCRIPTION OF THE OBJECT WILL BE PRINTED RATHER THAN THE DATA OF THE
OBJECT.
• CLASS PERSON:
• SELF.GENDER = GENDER
• • <__MAIN__.PERSON OBJECT
• PERSON1 = PERSON("JOHN", 30, "MALE") AT 0X0000029D0DBEF7F0>
• PERSON2 = PERSON("JANE", 25, "FEMALE")
• PRINT(PERSON1)
• PRINT(PERSON2)
EXAMPLE 3.
IN PYTHON WE HAVE ANOTHER METHOD NAMED __REPR__ TO PRINT THE
OBJECT WITH THE DETAILED VIEW. THIS METHOD IS AS SAME AS THE __STR__
METHOD BUT THE DIFFERENCE IS THAT, IT HANDLES MORE DETAILS OF THE
OBJECT.
• CLASS PERSON:
• SELF.NAME = NAME
• SELF.STATE = STATE
• OUTPUT
• SELF.PLACE = PLACE
• JOHN BELONGS TO ANDHRA
• DEF __REPR__(SELF):
PRADESH AND THE HOME
• RETURN F"{SELF.NAME} BELONGS TO {SELF.STATE}
TOWN OF JOHN IS
AND THE HOME TOWN OF {SELF.NAME} IS {SELF.PLACE}"
VISHAKHAPATNAM
•
• JANE BELONGS TO TELANGANA
• PERSON1 = PERSON("JOHN", "ANDHRA PRADESH", "MALE")
AND THE HOME TOWN OF JANE
• PERSON2 = PERSON("JANE", "TELANGANA", "FEMALE")
IS HYDERABAD .
• PRINT(PERSON1)
• PRINT(PERSON2)
EXAMPLE 4.
WHEN WE DIDN’T USE THE __REPR__ IN THE USER DEFINED CLASS AND IF WE
TRY TO PRINT THE OBJECT THEN THE OUTPUT OF THE PRINT STATEMENT WILL
BE THE DESCRIPTION OF THE OBJECT.
• CLASS PERSON:
• DEF __INIT__(SELF, NAME, STATE, GENDER):
• SELF.NAME = NAME
• OUTPUT
• PRINT(PERSON1)
• PRINT(PERSON2)
Printing objects give us information about the
objects we are working with. In C++, we can do
this by adding a friend ostream& operator <<
method for the class. In Java, we use toString()
method. In Python, this can be achieved by
using __repr__ or __str__ methods. __repr__ is
used if we need a detailed information for
debugging while __str__ is used to print a string
version for the users.
# PYTHON PROGRAM TO
DEMONSTRATE
# OBJECT PRINTING
# DEFINING A CLASS
CLASS TEST:
DEF __INIT__(SELF, A, B):
SELF.A = A
SELF.B = B
DEF __REPR__(SELF):
• OUTPUT :
DEF __STR__(SELF):
T = TEST(1234, 5678)
PRINT(T)
class Test:
def __init__(self, a, b):
self.a = a
self.b = b
def __repr__(self):
return "Test a:% s b:% s" % (self.a, self.b)
# Driver Code
t = Test(1234, 5678)
print(t)
Output:
class Test:
def __init__(self, a, b):
self.a = a
self.b = b
# Driver Code
t = Test(1234, 5678)
print(t)
Output:
name = "Alice"
age = 30
print("Hello, my name is", name, "and I am", age, "years old.")
item = "book"
price = 25.99
print(f"The {item} costs ${price:.2f}.")
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
book = Book("The Python Guide", "John Smith")
print(book)
print('Python Programming')
print(True)
print(1 + 5j)
my_list = [1, 2, 3, 4]
print(my_list)
Output :
12.35
Python Programming
True
(1+5j)
[1, 2, 3, 4]
{1: 'One', 2: 'Two', 3: 'Three'}
Consider a scenario involving a custom-made Person class:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
person = Person("Alice", 30)
print(f"Person's name: {person.name}")
print(f"Person's age: {person.age}")
Here, the print statements cast a radiant light on the attributes of the
person object. As you execute the code, a clear snapshot of the object's
inner state is unveiled, helping you pinpoint any discrepancies or issues.
Conclusion :