好的,我有这个代码:
class SomeClass:
@classmethod
def func1(cls,arg1):
#---Do Something---
@classmethod
def func2(cls,arg1):
#---Do Something---
# A 'function map' that has function name as its keys and the above function
# objects as values
func_map={'func1':func1,'func2':func2}
@classmethod
def func3(cls,arg1):
# following is a dict(created by reading a config file) that
# contains func names as keys and boolean as values that tells
# the program whether or not to run that function
global funcList
for func in funcList:
if funcList[func]==True:
cls.func_map[func](arg1) #TROUBLING PART!!! …
Run Code Online (Sandbox Code Playgroud) 除了标准[[MyClass alloc] init]
模式,一些对象是由静态方法构建的MyClass *obj = [MyClass classWithString:@"blabla"]
根据广泛的内存管理指南(包括Apple的),您只负责释放您的对象alloc
.
有人能为我提供这种方法的模板吗?你如何返回分配的对象([self alloc]; return self;
也许)?你如何确保它将被释放?
我以为我开始掌握编程的"Python方式".类的方法接受self作为第一个参数来引用正在调用该方法的上下文的类的实例.@ classmethod装饰器引用一个方法,其功能与类相关联,但不引用具体实例.
那么,如果没有实例引用调用该方法,那么@classmethod(规范'self')的第一个参数是什么意思?
我知道这self
是实例方法中的实例.那么,self
类方法中的类是什么?例如,以下是否会在Rails中工作?
class Post < ActiveRecord::Base
def self.cool_post
self.find_by_name("cool")
end
end
Run Code Online (Sandbox Code Playgroud) 假设我们有一个名为的类Calculator
.其中有一个类方法,叫做runProgram
.如果我想调用这个类方法,在类的实现中,这两者之间的区别是:
[Calculator runProgram]
Run Code Online (Sandbox Code Playgroud)
要么
[self runProgram]
Run Code Online (Sandbox Code Playgroud)
这两个都一样吗?
self.class.send :method, args...
当然不是.我想在类和实例级别上创建一个相当复杂的方法,而不需要复制代码.
更新:
@Jonathan Branam:这是我的假设,但我想确保没有其他人找到方法.Ruby中的可见性与Java中的可见性非常不同.你也很正确,private
它不适用于类方法,虽然这将声明一个私有类方法:
class Foo
class <<self
private
def bar
puts 'bar'
end
end
end
Foo.bar
# => NoMethodError: private method 'bar' called for Foo:Class
Run Code Online (Sandbox Code Playgroud) 在什么样的情况下代码:
module M
extend self
def greet
puts "hello"
end
end
Run Code Online (Sandbox Code Playgroud)
使用更有利于说:
module M
def self.greet
puts "hello"
end
end
Run Code Online (Sandbox Code Playgroud)
在顶部,一个是扩展的实例方法,后者只是一个类方法,但在调用任一方法时,你必须使用M.greet,对吧?我很好奇是否有人可以说明何时使用一个代码而不是另一个代码.谢谢!
例如,我有一个基类和一个派生类:
>>> class Base:
... @classmethod
... def myClassMethod(klass):
... pass
...
>>> class Derived:
... pass
...
>>> Base.myClassMethod()
>>> Derived.myClassMethod()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: class Derived has no attribute 'myClassMethod'
Run Code Online (Sandbox Code Playgroud)
是否有可能让Derived类能够调用myClassMethod而不覆盖它并调用super的类方法?我只想在必要时覆盖类方法.
假设以下课程:
class Class(object):
@classmethod
def getitem(*args):
print 'getitem %s' % (args,)
@classmethod
def __getitem__(*args):
print '__getitem__ %s' % (args,)
Run Code Online (Sandbox Code Playgroud)
getitem方法的行为符合预期:它Class
作为第一个arg __getitem__
接收,但type
作为第一个arg 接收:
calling Class.getitem(test)
getitem (<class '__main__.Class'>, 'test')
calling obj.getitem(test)
getitem (<class '__main__.Class'>, 'test')
calling Class[test]
'type' object has no attribute '__getitem__'
calling obj[test]
__getitem__ (<class '__main__.Class'>, 'test')
Run Code Online (Sandbox Code Playgroud)
背后有什么魔力__getitem__
?
我想只在加载一个类(而不是每个对象!)时合并当前和继承类的约束.
class Domain(Validatable):
constraints = {...}
Run Code Online (Sandbox Code Playgroud)
为此,我定义了一个_initialize_class_not_instance
应该为每个类调用一次的方法:
class Validatable:
@classmethod
def _initialize_class_not_instance(cls):
# merge constraints from derived class and base classes
pass
__class__._initialize_class_not_instance() # doesn't work
# Validatable._merge_constraints() # doesn't work too
Run Code Online (Sandbox Code Playgroud)
问题是__class__
在这种情况下不存在,Validatable
也没有定义.但是我想避免,我的API的用户必须明确地调用initialize方法或者必须使用额外的类装饰器.
任何想法如何初始化类?
class-method ×10
python ×5
ruby ×3
inheritance ×2
objective-c ×2
self ×2
class ×1
initializer ×1
instance ×1
module ×1
oop ×1
scope ×1
visibility ×1