About 142,000 results
Open links in new tab
  1. oop - What do __init__ and self do in Python? - Stack Overflow

    Jul 8, 2017 · __init__ is what we call a "constructor", meaning that it's a function which will create the object of the class, given the specified instructions from the parameters we pass into it, and given the "blueprint" - class. Note: Whenever we create an object of a specific class, the function that's been called first is the constructor.

  2. Why do we use __init__ in Python classes? - Stack Overflow

    Dec 23, 2011 · You're saying, Fido is a brown dog with 4 legs while Spot is a bit of a cripple and is mostly yellow. The __init__ function is called a constructor, or initializer, and is automatically called when you create a new instance of a class. Within that function, the newly created object is assigned to the parameter self.

  3. python - Calling a class function inside of __init__ - Stack Overflow

    Call the function in this way: self.parse_file() You also need to define your parse_file() function like this: def parse_file(self): The parse_file method has to be bound to an object upon calling it (because it's not a static method). This is done by calling the function on an instance of the object, in your case the instance is self.

  4. How to return a value from __init__ in Python? - Stack Overflow

    Mar 22, 2010 · Where DBEngineClass has the __new__ member function defined to return the pure ResultProxy from the engine call, whereas there is also a scalar() and fetchall() member function that relies on the __init__ member function to execute the sql query and then set a result member variable to the ResultProxy, which can then be mutated and returned by ...

  5. Inheritance and init method in Python - Stack Overflow

    In the first situation, Num2 is extending the class Num and since you are not redefining the special method named __init__() in Num2, it gets inherited from Num.

  6. python - __init__ as a constructor? - Stack Overflow

    Dive into Python-. It would be tempting but incorrect to call this the constructor of the class. It's tempting, because it looks like a constructor (by convention, __init__ is the first method defined for the class), acts like one (it's the first piece of code executed in a newly created instance of the class), and even sounds like one (“init” certainly suggests a constructor-ish nature).

  7. Inheritance and Overriding __init__ in python - Stack Overflow

    Read the answer carefully. It's all about intention. 1) If you want to leave the base class' init logic as is, you don't override init method in your derived class. 2) If you want to extend the init logic from the base class, you define your own init method and then call base class' init method from it.

  8. class - __new__ and __init__ in Python - Stack Overflow

    Nov 13, 2011 · Both are just parameter names with no special meaning in the language. But their use is a very strong convention in the Python community; most Pythonistas will never change the names self and cls in these contexts and will be confused when someone else does. Note that your use of def __new__(tuple) re-binds the name tuple inside the constructor ...

  9. How to call Base Class's __init__ method from the child class?

    Oct 6, 2013 · If I have a python class as: class BaseClass(object): #code and the init function of the base class And then I define a child class such as: class ChildClass(BaseClass): #here I want to call the init function of the base class

  10. python - Can I use __init__.py to define global variables ... - Stack ...

    Apr 5, 2017 · I want to define a constant that should be available in all of the submodules of a package. I've thought that the best place would be in in the __init__.py file of the root package.