Python Forum
init vs_init_ while defining method/function?
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
init vs_init_ while defining method/function?
#1
while defining method,what is the difference in
this:
def init(...........):

      ................................
 
and 
this:
def _init_(............):
     ................................
Reply
#2
In that case, it's just a different name. But __init__ (note the double underscores on both sides, or "dunder") is called automatically when you create an object.
Output:
>>> class Manual: def init(self): self.x = None >>> m = Manual() >>> print m.x Traceback (most recent call last):  File "<pyshell#3>", line 1, in <module>    print m.x AttributeError: Manual instance has no attribute 'x' >>> m.init() >>> print m.x None >>> class Automatic: def __init__(self): self.x = None >>> a = Automatic() >>> print a.x None
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  i want to use type= as a function/method keyword argument Skaperen 9 1,833 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 1,035 Sep-19-2022, 02:32 AM
Last Post: Xeno
  Init an indefinite number of class MathisDELAGE 9 2,275 Feb-18-2022, 07:49 PM
Last Post: deanhystad
  Basic Inheritance, Why Use init udinjelek 5 2,168 Sep-29-2021, 06:03 PM
Last Post: deanhystad
  Conjugate Gradient having issues with defining A (function to solve [A]{x} = {b} ) DimosG 2 2,819 Sep-21-2021, 08:32 PM
Last Post: 1968Edwards
  Defining a function with input abcd 5 3,095 Feb-21-2021, 02:34 AM
Last Post: NullAdmin
  Building a method name in a function ffgth 9 3,186 Oct-19-2020, 01:21 PM
Last Post: buran
  Error: How to to close and restart your shell after running 'conda init' angelica 3 10,250 May-27-2020, 10:00 AM
Last Post: snippsat
  function/method help myv5285 3 2,784 May-17-2020, 04:19 AM
Last Post: buran
  When Defining a Function with an Equation as a Default Argument, which Value Is Used? OJGeorge4 4 2,649 Apr-09-2020, 08:48 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020