__init__() & __new__() in python

# __init__() behaves like a constructor and invoked automatically at the time of object declaration along with initialization of all the required members with default values or values provided at the time of declaration of object


class Motivation:
? ? def __init__(self):
? ? ? ? print("we have initialized our journey")


#object declaration? ? ? ??
motive = Motivation()


#output
we have initialized our journey


# __new__() method will be called whenever a object is created before the __init__() method to create the object taking "cls" as a parameter which is the class which needs to be instantiated.

class Organization:
? ? def __new__(cls):
? ? ? ? print("we are looking forward to be stable with unstable thoughts")
? ? ? ? return super(Organization, cls).__new__(cls)
? ? ? ??
? ? def __init__(self):
? ? ? ? print("we need self motivating employees with demotivated environment")


#object declaration?
employee = Organization()


#output
we are looking forward to be stable with unstable thoughts
we need self motivating employees with demotivated environment


# __new__ without return
class Employee:
? ? def __new__(cls):
? ? ? ? print("need growth in every possible way along with self respect")
? ? ? ? print("without returning proper input, can't get proper output everytime on intialization")
? ? ? ??
? ? def __init__(self):
? ? ? ? print("unable to give output")


#object declaration?
growth = Employee()
print(growth)


#output
need growth in every possible way along with self respect
without returning proper output, can't get proper input everytime on
intialization
None


# __init__ method is called every time an instance of the class is returned by __new__ method
        

要查看或添加评论,请登录

Harshit Jain的更多文章

  • positional only & keyword only arguments

    positional only & keyword only arguments

    # with using "/" you can represent parameters before it as positional argments and # with using "*" you can…

  • Build your own iterator

    Build your own iterator

    # Define your own iteraotr by using __iter__ & __next__ functions. class OddNumbers: def __iter__(self):…

  • "sep" & "end" parameter in print()

    "sep" & "end" parameter in print()

    #Their is one more cool way you can use to represent your print() function outputs. Default is to separate them with a…

  • Packing & Unpacking in python

    Packing & Unpacking in python

    # we can unpack iterables to variables in a single assignment using automatic unpacking. Below is an example of…

  • Python 3.5+ type annotations

    Python 3.5+ type annotations

    # Python 3.5+ supports 'type annotations' that can b # used with tools like Mypy to write statically typed Python: def…

  • Peeking Behind The Bytecode Curtain

    Peeking Behind The Bytecode Curtain

    # You can use Python's built-in "dis # module to disassemble functions and # inspect their CPython VM bytecode: >>>…

  • contextlib.suppress

    contextlib.suppress

    # In Python 3.4+ you can use # contextlib.

  • Python list slice syntax fun

    Python list slice syntax fun

    # Python's list slice syntax can be used without indices # for a few fun and useful things: # You can clear all…

  • When To Use __repr__ vs __str__?

    When To Use __repr__ vs __str__?

    # When To Use __repr__ vs __str__ # Emulate what the std lib does: >>> import datetime >>> today = datetime.date.

  • Merging two dicts in Python 3.5+ with a single expression

    Merging two dicts in Python 3.5+ with a single expression

    # How to merge two dictionaries # in Python 3.5+ >>> x = {'a': 1, 'b': 2} >>> y = {'b': 3, 'c': 4} >>> z = {**x, **y}…

    1 条评论

社区洞察

其他会员也浏览了