Questions tagged [init]

May refer to Linux init, an abbreviation of initialization - giving variables a "starting" value, or Python's __init__ class initiation method.

May refer to :

  • Linux Init - the parent of all processes, which primary role is to create processes from a script stored in the file /etc/inittab.
  • variable init (abbreviation of initialization) - giving variables a "starting" value.
  • python magic __init__ - magic function that initializes an object.
1905 questions
61
votes
5 answers

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 like this: class SomeMetaClass(type): def…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
57
votes
3 answers

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 before all stored properties are initialized }…
norains
  • 743
  • 1
  • 5
  • 12
47
votes
6 answers

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__ = ['foo'] foo = {'bar': 'baz'} How can I access foo…
sharvey
  • 7,635
  • 7
  • 48
  • 66
45
votes
1 answer

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?
Dribbel
  • 2,060
  • 2
  • 17
  • 29
44
votes
2 answers

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) airpc@192 InstaClone % pod…
iOSDeveloper
  • 475
  • 1
  • 3
  • 7
44
votes
3 answers

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 initialization logic? @implementation…
hpique
  • 119,096
  • 131
  • 338
  • 476
43
votes
2 answers

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. I have tried NSDecimalNumber* d = [aNumber…
Michael Rivers
  • 920
  • 2
  • 10
  • 17
42
votes
5 answers

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. First of all, I've read the question here, where…
Mister Smith
  • 27,417
  • 21
  • 110
  • 193
39
votes
6 answers

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 on Ubuntu/Debian, but on RHEL the script which is…
ddario
  • 1,015
  • 3
  • 12
  • 25
38
votes
3 answers

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]])) { self.backgroundColor = [UIColor…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
36
votes
4 answers

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 the http module init method is being called…
MartinF
  • 5,929
  • 5
  • 40
  • 29
34
votes
1 answer

__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 names: Project/ Animals/ __init__.py …
Darren McAffee
  • 645
  • 1
  • 6
  • 10
34
votes
5 answers

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?? ... should dict.__init__(self) be called,…
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
34
votes
3 answers

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 init?
Sandeep
  • 18,356
  • 16
  • 68
  • 108
33
votes
8 answers

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 ├── __init__.py └── main.py And this is the error…
Nicolas
  • 331
  • 1
  • 3
  • 5