1 Answer

0 votes
by

__init__ is a contructor method in Python and is automatically called to allocate memory when a new object/instance is created. All classes have a __init__ method associated with them. It helps in distinguishing methods and attributes of a class from local variables.

# class definition
class Student:
    def __init__(self, fname, lname, age, section):
        self.firstname = fname
        self.lastname = lname
        self.age = age
        self.section = section

# creating a new object
stu1 = Student("Sara", "Ansh", 22, "A2")

Related questions

0 votes
    What will be the output of the following Python code? class tester: def __init__(self, id): self.id = str(id) id="224" >>>temp ... >>>print(temp.id) a) 12 b) 224 c) None d) Error...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python code? class tester: def __init__(self, id): self.id = str(id) id="224" >>>temp ... >>>print(temp.id) a) 12 b) 224 c) None d) Error...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    I'm learning the Python programming language and I've come across something I don't fully understand. In a method ... know very much. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I am a learner and just want to know the basic concept of super(). Here in the given code I want to know ... () ChildOne() ChildTwo() Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I've recently seen quite a bit of the following pattern in Python: class Foo: pass class Bar: ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I'm trying to follow PEP 328, with the following directory structure: pkg/ __init__.py components/ Core.py __init__ ... m missing here? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    What will be the output of the following Python code? >>>list1 = [1, 3] >>>list2 = list1 >>>list1[0] = 4 >>>print(list2) a) [1, 4] b) [1, 3, 4] c) [4, 3] d) [1, 3]...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python code? print('*', "abcde".center(6), '*', sep='') a) * abcde * b) *abcde * c) * abcde* d) * abcde *...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    To add a new element to a list we use which Python command? a) list1.addEnd(5) b) list1.addLast(5) c) list1.append(5) d) list1.add(5)...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the value of ‘result’ in following Python program? list1 = [1,2,3,4] list2 = [2,4,5,6] list3 = [2,6,7,8] result ... ) [1, 7, 8] c) [1, 2, 4, 7, 8] d) error...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    Which of the following statements is used to create an empty set in Python? a) ( ) b) [ ] c) { } d) set()...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python code? print("abc. DEF".capitalize()) a) Abc. def b) abc. def c) Abc. Def d) ABC. DEF...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What arithmetic operators cannot be used with strings in Python? a) * b) – c) + d) All of the mentioned...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python program? z=set('abc') z.add('san') z.update(set(['p', 'q'] ... ;, ‘c’, [‘p’, ‘q’], ‘san}...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    Which module in the python standard library parses options received from the command line? a) getarg b) getopt c) main d) os...
asked Jan 2, 2023 in Technology by JackTerrance
...