Questions tagged [init]
May refer to Linux init, an abbreviation of initialization - giving variables a "starting" value, or Python's __init__ class initiation method.
init
1,929
questions
842
votes
23
answers
664k
views
How to fix "Attempted relative import in non-package" even with __init__.py
I'm trying to follow PEP 328, with the following directory structure:
pkg/
__init__.py
components/
core.py
__init__.py
tests/
core_test.py
__init__.py
In core_test.py I have ...
544
votes
11
answers
252k
views
When does the init() function run?
I've tried to find a precise explanation of what the init() function does in Go. I read what Effective Go says but I was unsure if I understood fully what it said. The exact sentence I am unsure is ...
191
votes
4
answers
144k
views
Can I use __init__.py to define global variables?
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. But I don't know ...
166
votes
14
answers
255k
views
How to return a value from __init__ in Python?
I have a class with an __init__ function.
How can I return an integer value from this function when an object is created?
I wrote a program, where __init__ does command line parsing and I need to ...
166
votes
9
answers
41k
views
In Objective-C why should I check if self = [super init] is not nil?
I have a general question about writing init methods in Objective-C.
I see it everywhere (Apple's code, books, open source code, etc.) that an init method should check if self = [super init] is not ...
156
votes
6
answers
107k
views
git add all except ignoring files in .gitignore file
I am adding source control to a project that had none. The problem is that there are a lot of files to initially add to git with a .gitignore file, but I can't figure out how to add all files without ...
132
votes
5
answers
187k
views
Inheritance and init method in Python
I'm begginer of python. I can't understand inheritance and __init__().
class Num:
def __init__(self,num):
self.n1 = num
class Num2(Num):
def show(self):
print self.n1
...
92
votes
4
answers
295k
views
What is the use of the init() usage in JavaScript?
What is the meaning and usage of the init() function in JavaScript?
86
votes
7
answers
93k
views
Python: Inherit the superclass __init__
I have a base class with a lot of __init__ arguments:
class BaseClass(object):
def __init__(self, a, b, c, d, e, f, ...):
self._a=a+b
self._b=b if b else a
...
All the ...
81
votes
17
answers
157k
views
git add . -> still "nothing to commit" with new files
I am struggling with Git, I can't seem to add my files. I ran ls to show that the files are in the current directory, then ran git add . then git status which showed "nothing to commit".
JJ-Computer:...
78
votes
2
answers
28k
views
Objective-C: init vs initialize
In Objective-C, what is the difference between the init method (i.e. the designated initializer for a class) and the initialize method? What initialization code should be put in each?
78
votes
3
answers
40k
views
How critical is dumb-init for Docker?
I hope that this question will not be marked as primarily opinion-based, but that there is an objective answer to it.
I have read Introducing dumb-init, an init system for Docker containers, which ...
75
votes
3
answers
115k
views
Property initializers run before 'self' is available
Seems like I'm having a problem with something that shouldn't be the case... But I would like to ask for some help.
There are some explanations here on the Stack I don't get.
Having two simple classes ...
74
votes
8
answers
38k
views
Guice call init method after instantinating an object
Is it possible to tell Guice to call some method (i.e. init()) after
instantinating an object of given type?
I look for functionality similar to @PostConstruct annotation in EJB 3 (and Spring).
72
votes
3
answers
36k
views
What is the difference between [Class new] and [[Class alloc] init] in iOS? [duplicate]
Possible Duplicate:
alloc, init, and new in Objective-C
I am a little confused about [Class new] and [[Class alloc] init]. I have defined an object content using [Class new] and [[Class alloc] ...
61
votes
5
answers
19k
views
Is there any reason to choose __new__ over __init__ when defining a metaclass?
I've always set up metaclasses something like this:
class SomeMetaClass(type):
def __new__(cls, name, bases, dict):
#do stuff here
But I just came across a metaclass that was defined ...
60
votes
3
answers
18k
views
How could I initialize the @State variable in the init function in SwiftUI?
Let's see the simple source code:
import SwiftUI
struct MyView: View {
@State var mapState: Int
init(inputMapState: Int)
{
mapState = inputMapState //Error: 'self' used ...
47
votes
5
answers
29k
views
Pod Init Error: "force_encoding': can't modify frozen String (FrozenError)" - at iOS
I encounter this error when I issue a "pod init" command for a project. Terminal is set to "Open Using Rosetta".
Images:
Open using Rosetta
pod init error
Thanks for helps.
(base) ...
47
votes
6
answers
15k
views
Using module's own objects in __main__.py
I’m trying to access a module’s data from inside its __main__.py.
The structure is as follows:
mymod/
__init__.py
__main__.py
Now, if I expose a variable in __init__.py like this:
__all__ =...
46
votes
1
answer
40k
views
How to set which control gets the focus on application start
For a C# Windows Forms application, how do I set the default focus to a given control when my application starts?
44
votes
3
answers
62k
views
Overriding init in subclass
In Objective-C, is it necessary to override all inherited constructors of a subclass to add custom initialization logic?
For example, would the following be correct for a UIView subclass with custom ...
43
votes
2
answers
22k
views
Convert NSNumber to NSDecimalNumber
I realize that NSDecimalNumber inherits from NSNumber.
However, I am struggling to figure out how I can init an NSDecimalNumber from an NSNumber or how to convert an NSNumber to an NSDecimalNumber.
...
42
votes
5
answers
6k
views
Best way of preventing other programmers from calling -init
When designing a class hierarchy, sometimes the subclass has added a new initWithSomeNewParam method, and it would be desirable to disable calls to the old init method inherited from the superclass.
...
41
votes
8
answers
214k
views
Python - ModuleNotFoundError: No module named
I'm new in Python and I'm having the following error with this simple example:
This is my project structure:
python_project
.
├── lib
│ ├── __init__.py
│ └── my_custom_lib.py
└── src
├── ...
40
votes
6
answers
178k
views
How to run a command as a specific user in an init script?
I'm writing an init script which is supposed to execute a single command as a user different than root. This is how I'm doing it currently:
sudo -u username command
This generally works as expected ...
38
votes
3
answers
71k
views
iOS: UIView subclass init or initWithFrame:?
I made a subclass of UIView that has a fixed frame. So, can I just override init instead of initWithFrame:? E.g.:
- (id)init {
if ((self = [super initWithFrame:[[UIScreen mainScreen] bounds]])) {
...
38
votes
4
answers
23k
views
HttpModule Init method is called several times - why?
I was creating a http module and while debugging I noticed something which at first (at least) seemed like weird behaviour.
When I set a breakpoint in the init method of the httpmodule I can see that ...
38
votes
1
answer
72k
views
__init__.py can't find local modules
Borrowing a simplified example at http://pythoncentral.io/how-to-create-a-python-package/
I have an analogous file structure as follows, where Mammals.py and Birds.py define classes with the same ...
36
votes
3
answers
35k
views
module_init() vs. core_initcall() vs. early_initcall()
In drivers I often see these three types of init functions being used.
module_init()
core_initcall()
early_initcall()
Under what circumstances should I use them?
Also, are there any other ways of ...
34
votes
5
answers
25k
views
Subclassing dict: should dict.__init__() be called?
Here is a twofold question, with a theoretical part, and a practical one:
When subclassing dict:
class ImageDB(dict):
def __init__(self, directory):
dict.__init__(self) # Necessary??
...
33
votes
4
answers
44k
views
How to write init method in Swift?
I want to write an init method in Swift. Here I initialize an NSObject class in Objective-C:
-(id)initWithNewsDictionary:(NSDictionary *)dictionary
{
self = [super init];
if (self) {
...
33
votes
4
answers
54k
views
What is initState and super.initState in flutter?
In the documentation it is written but I am not able to understand it.
Called when this object is inserted into the tree.
The framework will call this method exactly once for each State object it ...
29
votes
3
answers
24k
views
Python multi-inheritance, __init__
Regarding multiple parent inheritance, when I call the super.__init__, why doesn't parent2's __init__ function get called? Thanks.
class parent(object):
var1=1
var2=2
def __init__(self,x=...
26
votes
2
answers
19k
views
What is the meaning of npm init -y
What is the use of the flag -y in the npm init command? I noticed it is used in setting up an npm project, but what exactly does it mean?
npm init -y
26
votes
5
answers
57k
views
Why should we use -> in def __init__(self, n) -> None:?
Why should we use -> in def __init__(self, n) -> None:? I read the following excerpt from PEP 484, but I am unable to understand what it means.
(Note that the return type of __init__ ought to ...
24
votes
1
answer
12k
views
Swift: Calling an init from another init
I have an object with two init methods. One of them takes in a NSDictionary, and the other takes in a whole ton of String variables. I want to call the NSDictionary init, and from there convert my ...
24
votes
2
answers
12k
views
What's the purpose of golang allowing multiple init in one package?
I know that golang allows multiple init in one package and even in one file.
I am wondering why?
For example, if a pkg has many files, we could write multiple init then we could get lost in where to ...
24
votes
3
answers
31k
views
iOS loadNibNamed confusion, what is best practice?
I'm familiar with most of the process of creating an XIB for my own UIView subclass, but not everything is working properly for me - it's mostly to do with the IBOutlets linking up. I can get them to ...
24
votes
3
answers
28k
views
What does object's __init__() method do in python? [duplicate]
While reading the code of OpenStack and I encountered this.
A class named 'Service' inherits the base class 'object', and then in Service's __init__() method, object's __init__ is called. The related ...
23
votes
3
answers
25k
views
calling init for multiple parent classes with super? [duplicate]
Possible Duplicate:
Can Super deal with multiple inheritance?
Python inheritance? I have a class structure (below), and want the child class to call the __init__ of both parents. Is this possible ...
23
votes
1
answer
23k
views
Optional parameter in class initialization
I'm working with Swift, Sprite-Kit and Xcode 6,
I have a class declared like this :
class Obstacles: SKSpriteNode
{
init(initTime: Int, speed: CGFloat, positionX: CGFloat, rotationSpeed: CGFloat)...
22
votes
1
answer
23k
views
Emacs init.el file doesn't load
When I open emacs, the following messages appear in the *Messages* buffer, and my init.el file (located at ~/.emacs.d/init.el) doesn't load.
Loading 00debian-vars...done
Loading /etc/emacs/site-...
21
votes
1
answer
21k
views
'Use of self in method call before super.init initializes self', can't init properties through a method call
I'm curious is there is anyway to call a method inside your init method that sets instance properties of the class. Essentially I just have a class that sub-classes UIView, adds some subviews in init,...
21
votes
1
answer
14k
views
Convenience Init Override
Problem
Override a convenience initializer of a subclass and it produces a compile error.
Detail
I am having issues understanding why Swift (v4.1) is not letting me override my convenience ...
20
votes
3
answers
21k
views
swift - Initialize view controller from storyboard by overriding init
I have a ViewController instance defined in a storyboard. I can initialize it by the following
var myViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("...
20
votes
1
answer
34k
views
Passing parameters to a custom class on initialization
I have a class Message with two attributes, name and message, and another class MessageController with two text fields, nameField and messageField.
I want to make an instance of Message in ...
19
votes
4
answers
32k
views
IOS: NSMutableArray initWithCapacity
I have this situation
array = [[NSMutableArray alloc] initWithCapacity:4]; //in viewDidLoad
if (index == 0){
[array insertObject:object atIndex:0];
}
if (index == 1){
[array insertObject:...
19
votes
7
answers
16k
views
Pycharm (Python IDE) doesn't auto complete Django modules
My Python IDE (pycharm) has stopped auto completing my modules (suggestions). I get unresolved references after every django module I try to import so:
from django - works, however soon as I add a '...
17
votes
2
answers
15k
views
TypeScript init: error TS6053: File 'init.ts' not found
If I run tsc init in a empty directory, I get the error:
error TS6053: File 'init.ts' not found
I have an understanding question: Shouldn't tsc init simply create a tsconfig.json file? If not, ...
17
votes
5
answers
10k
views
Generate custom init method automatically
Is it possible to generate custom init method automatically from Xcode, as Android Studio does for android?
I mean, if I declare some properties in .h, for example:
int a;
int b;
So, I ...