self

'self' seems to be hogging one of my arguments

拈花ヽ惹草 提交于 2019-12-23 05:45:22
问题 I am trying to learn object orientated programming in python 3. I am making a variation of a notebook program that I have in a book but instead of adding notes to a notebook I am trying to add days to a timesheet. In the original tutorial, this is in the main program: def add_note(self): memo = input("Enter a memo: ") self.notebook.new_note(memo) print("Your note has been added") and this is in the class module (notebook): def new_note(self, memo, tags = ''): '''create a new note and add it

Will self retain within block?

末鹿安然 提交于 2019-12-22 17:58:59
问题 Before/After call the block, the retaincount is always 1. From apple block doc we know that the self should retain. Can anyone know why? NSLog(@"Before block retain count: %d", [self retainCount]); void (^block)(void) = ^(void){ UIImage* img = [UIImage imageNamed:@"hometown.png"]; [self setImage:img]; NSLog(@"After block retain count: %d", [self retainCount]); }; block(); 回答1: First, retainCount is useless. Don't call it. . Blocks only retain captured objects when the block is copied. Thus,

clickedButtonAtIndex in appdelegate is not called

荒凉一梦 提交于 2019-12-22 08:03:12
问题 I am calling UIAlert with 2 buttons "Cancel" and "OK" in MyapplicationAppDelegate.m file , the alert is called but on tap of "Cancel" or "OK" button -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex method is not called. I have added UIAlertViewDelegate in the MyapplicationAppDelegate.h file as below #import UIKit/UIKit.h @interface MyapplicationAppDelegate: NSObject UIApplicationDelegate,UIAlertViewDelegate { .. } I want to know what else is required. 回答1:

When to use `self` in Objective-C?

风流意气都作罢 提交于 2019-12-22 07:57:16
问题 It's now more than 5 months that I'm in Objective-C, I've also got my first app published in the App Store, but I still have a doubt about a core functionality of the language. When am I supposed to use self accessing iVars and when I'm not? When releasing an outlet you write self.outlet = nil in viewDidUnload, instead in dealloc you write [outlet release] . Why? 回答1: When you write self.outlet = nil the method [self setOutlet:nil]; is called. When you write outlet = nil; you access variable

Why 'self.self' compiles and run in swift?

倖福魔咒の 提交于 2019-12-22 06:36:53
问题 Yesterday I reviewed a piece of code in Swift which included this line: self.self.someProperty Which surprised me, because the word self is reserved and used as a reference to the current instance. At first I checked for that phenomenon in other languages, but all gave errors. Which wasn't a surprise - but still, why in swift does it compile and run? Second I searched in the internet about this and haven't found anything relevant... Edit I reproduced that and from my checks: self.someProperty

Lua: colon notation, 'self' and function definition vs. call

限于喜欢 提交于 2019-12-22 05:52:49
问题 I'm getting terribly confused by the colon notation used when defining/calling Lua functions. I thought I'd got my head round it until I saw this piece of code: function string.PatternSafe( str ) return ( str:gsub( ".", pattern_escape_replacements ) ); end function string.Trim( s, char ) if char then char = char:PatternSafe() else char = "%s" end return string.match( s, "^" .. char .. "*(.-)" .. char .. "*$" ) or s end What's confusing me here is that string.PatternSafe() doesn't reference

Why use [ClassName alloc] instead of [[self class] alloc]?

南楼画角 提交于 2019-12-21 03:25:18
问题 I'm reading through Mark Dalrymple's Learn Objective-C on the Mac (only at the chapter on Protocols, so still relatively newbish) and trying to figure something out: Why would you ever reference a class by its own name? If I had a class called Foo , why would I ever want to write, say, [[Foo alloc] init] and not [[[self class] alloc] init] If I had a subclass Bar, wouldn't the first option invalidate me from writing [[Bar alloc] init] whereas the second option would allow it? When would the

Ruby self keyword

孤人 提交于 2019-12-20 15:26:23
问题 im having trouble understanding the self keyword . I get how it's used to distinguish between Instance Methods and Class Methods but what about when it's used from inside a method. Something like def self.name self.name = "TEXT" end or def name2 self.name = "TEXT2" end or class Array def iterate!(&code) self.each_with_index do |n, i| self[i] = code.call(n) end end end 回答1: Usually, self as a receiver can be omitted, and in such cases, it is usually preferable to do so. However, there are a

Python - Is it okay to pass self to an external function

☆樱花仙子☆ 提交于 2019-12-20 10:18:12
问题 I have a class, A, which is inherited by a bunch of other classes. Some of these have a few functions which are similar and it would be nice to have those functions defined somewhere else and called by the classes that need them. But those functions call functions defined in the super class. class A(): def imp_func(*args): # called by the child class functions Class B(A): def common_func(self): # some stuff self.imp_func(*args) So I have created my helper functions which take the self object

What is the purpose of checking self.__class__ ? - python

限于喜欢 提交于 2019-12-20 09:37:22
问题 What is the purpose of checking self.__class__ ? I've found some code that creates an abstract interface class and then checks whether its self.__class__ is itself, e.g. class abstract1 (object): def __init__(self): if self.__class__ == abstract1: raise NotImplementedError("Interfaces can't be instantiated") What is the purpose of that? Is it to check whether the class is a type of itself? The code is from NLTK's http://nltk.googlecode.com/svn/trunk/doc/api/nltk.probability-pysrc.html