init

Using module's own objects in __main__.py

跟風遠走 提交于 2019-12-02 20:02:15
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 from __main__.py ? pdemb You need to either have the package already in sys.path , add the directory containing mymod to sys.path in __main__.py , or use the -m switch. To add mymod to the path would look something like this (in __main__.py ): import sys import os path = os.path.dirname(sys.modules[__name__].__file__) path = os.path.join(path, '..') sys

Calling dealloc in init?

谁说胖子不能爱 提交于 2019-12-02 19:30:41
问题 I am writing a framework and I have an object with a custom init method: @implementation OSDatabase @synthesize database; // MEM - (void)dealloc { sqlite3_close(database); [super dealloc]; } // INIT - (id)initWithDatabasePath:(NSString *)path error:(NSError **)error { if (self = [super init]) { if (!sqlite3_open_v2([path UTF8String], &database, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)) { error = [NSError errorWithDomain:@"OSDatabaseErrorDomain" code:1 userInfo:nil]; [self dealloc];

Syntax error of “;; unexpected” on simple init script for Debian

守給你的承諾、 提交于 2019-12-02 18:04:56
问题 This question was migrated from Server Fault because it can be answered on Stack Overflow. Migrated 4 years ago . I have this init script to run uwsgi. It works, but only on the start command. All the other commands give me this error: /etc/init.d/uwsgi: 27: /etc/init.d/uwsgi: Syntax error: ";;" unexpected It seems like the colons should be there from the tutorials I am reading, and yet it is telling me to remove them? #!/bin/sh ### BEGIN INIT INFO # Provides: uwsgi # Required-Start: $local

Python multi-inheritance, __init__

元气小坏坏 提交于 2019-12-02 17:32:00
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=1,y=2): self.var1=x self.var2=y class parent2(object): var4=11 var5=12 def __init__(self,x=3,y=4): self.var4=x self.var5=y def parprint(self): print self.var4 print self.var5 class child(parent, parent2): var3=5 def __init__(self,x,y): super(child, self).__init__(x,y) childobject = child(9,10) print childobject.var1 print childobject.var2 print childobject.var3 childobject.parprint() Output is 9 10 5 11 12

Instance member cannot be used on type Class?

喜欢而已 提交于 2019-12-02 14:34:44
问题 I'm trying to access the name variable from Indicator class, which inherits from Person. However, I believe I'm not doing my initialization right. I get the following: 'error: instance member 'name' cannot be used on type 'Indicator'`. class Person { var name: String init(myName: String){ self.name = myName } deinit { Indicator.letKnowPersonDeinitialized() } } class Indicator: Person { convenience init() { self.init() } static func letKnowPersonDeinitialized() { print("\(name)") } } 回答1: You

What is the use of the init() usage in JavaScript?

怎甘沉沦 提交于 2019-12-02 14:02:57
So, I just wanna know if anybody knows the particular meaning and usage of the init() statement in JavaScript, never really knew, kind of a newb. JavaScript doesn't have a built-in init() function, that is, it's not a part of the language. But it's not uncommon (in a lot of languages) for individual programmers to create their own init() function for initialisation stuff. A particular init() function may be used to initialise the whole webpage, in which case it would probably be called from document.ready or onload processing, or it may be to initialise a particular type of object, or...well,

Instance member cannot be used on type Class?

流过昼夜 提交于 2019-12-02 12:38:44
I'm trying to access the name variable from Indicator class, which inherits from Person. However, I believe I'm not doing my initialization right. I get the following: 'error: instance member 'name' cannot be used on type 'Indicator'`. class Person { var name: String init(myName: String){ self.name = myName } deinit { Indicator.letKnowPersonDeinitialized() } } class Indicator: Person { convenience init() { self.init() } static func letKnowPersonDeinitialized() { print("\(name)") } } You cannot access non-static stuff directly in a static method. The method letKnowPersonDeinitialized is static

Calling dealloc in init?

主宰稳场 提交于 2019-12-02 11:39:06
I am writing a framework and I have an object with a custom init method: @implementation OSDatabase @synthesize database; // MEM - (void)dealloc { sqlite3_close(database); [super dealloc]; } // INIT - (id)initWithDatabasePath:(NSString *)path error:(NSError **)error { if (self = [super init]) { if (!sqlite3_open_v2([path UTF8String], &database, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)) { error = [NSError errorWithDomain:@"OSDatabaseErrorDomain" code:1 userInfo:nil]; [self dealloc]; return nil; } } return self; } @end Is it safe to call dealloc inside of an init method if an error

What is the difference writing code in a class and in def __init__(self) in Python? [duplicate]

心不动则不痛 提交于 2019-12-02 06:38:43
Possible Duplicate: Variables inside and outside of a class __init__() function I understand that when a class is called it will run the code in __init__ before anything. I still don't see the difference between that, and writing the code directly under the class. For example: class main(): x = 1 def disp(self): print self.x class main(): def __init__(self): self.x = 1 def disp(self): print self.x To me, both have the same functionality. (Maybe I am missing out something.) I would like to know which is more (ahem) pythonic and why. There are a couple key differences here, both between __init__

Python, __init__ and self confusion

拜拜、爱过 提交于 2019-12-01 20:49:23
Alright, so I was taking a look at some source when I came across this: >>> def __parse(self, filename): ... "parse ID3v1.0 tags from MP3 file" ... self.clear() ... try: ... fsock = open(filename, "rb", 0) ... try: ... fsock.seek(-128, 2) ... tagdata = fsock.read(128) ... finally: ... fsock.close() ... if tagdata[:3] == 'TAG': ... for tag, (start, end, parseFunc) in self.tagDataMap.items(): ... self[tag] = parseFunc(tagdata[start:end]) ... except IOError: ... pass ... So, I decided to test it out. >>> __parse("blah.mp3") And, I received this error: Traceback (most recent call last): File "