Do you think declarations within Python class are equivalent to those within __init__ method?

PythonServer Side ProgrammingProgramming

Declarations anywhere in the class(other than in __init__) and declarations in the __init__method are not the same. The following code shows this to be true.

Example

import sys
class foo():
    print 'within class'
    def __init__(self):
        print 'within init'
    def do_smthng(self):
        print 'do something'

def main():
    f=foo()
    f.do_smthng()
    return 0
if __name__ == '__main__':
    sys.exit( main() )

Output

within class
within init
do something
raja
Published on 25-Jan-2018 10:21:31
Advertisements