which class gets declared automatically in the middle of the of the expression automatically and can be public , private in python code example
Example 1: class in python
class ComplexNumber:
def __init__(self, r=0, i=0):
self.real = r
self.imag = i
def get_data(self):
print(f'{self.real}+{self.imag}j')
num1 = ComplexNumber(2, 3)
num1.get_data()
num2 = ComplexNumber(5)
num2.attr = 10
print((num2.real, num2.imag, num2.attr))
print(num1.attr)
Example 2: how to write a class with inputs in python
class Person:
"""
A representation of a person
Attributes:
Firstname(string)
Lastname(String)
"""
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
def show_full_name(self):
return self.firstname + ' ' + self.lastname
@classmethod
def get_user_input(self):
while 1:
try:
firstname = input('Enter first name: ')
lastname = input('Enter last name')
return self(firstname,lastname)
except:
print('Invalid input!')
continue
person3 = Person.get_user_input()
person3.show_full_name()