标签: class-method

'classmethod'对象不可调用

好的,我有这个代码:

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)

python class-method

21
推荐指数
3
解决办法
2万
查看次数

创建新实例的类方法

除了标准[[MyClass alloc] init]模式,一些对象是由静态方法构建的MyClass *obj = [MyClass classWithString:@"blabla"]

根据广泛的内存管理指南(包括Apple的),您只负责释放您的对象alloc.

有人能为我提供这种方法的模板吗?你如何返回分配的对象([self alloc]; return self;也许)?你如何确保它将被释放?

memory-management objective-c initializer class-method

20
推荐指数
1
解决办法
1万
查看次数

"自我"在@classmethod中引用了什么?

我以为我开始掌握编程的"Python方式".类的方法接受self作为第一个参数来引用正在调用该方法的上下文的类的实例.@ classmethod装饰器引用一个方法,其功能与类相关联,但不引用具体实例.

那么,如果没有实例引用调用该方法,那么@classmethod(规范'self')的第一个参数是什么意思?

python self class-method

19
推荐指数
2
解决办法
1万
查看次数

在Ruby中,在类方法中,自我是类还是实例?

我知道这self是实例方法中的实例.那么,self类方法中的类是什么?例如,以下是否会在Rails中工作?

class Post < ActiveRecord::Base
  def self.cool_post
    self.find_by_name("cool")
  end
end
Run Code Online (Sandbox Code Playgroud)

ruby class self instance class-method

18
推荐指数
3
解决办法
1万
查看次数

通过类名与self调用类方法

假设我们有一个名为的类Calculator.其中有一个类方法,叫做runProgram.如果我想调用这个类方法,在类的实现中,这两者之间的区别是:

[Calculator runProgram]
Run Code Online (Sandbox Code Playgroud)

要么

[self runProgram]
Run Code Online (Sandbox Code Playgroud)

这两个都一样吗?

objective-c class-method

18
推荐指数
1
解决办法
2万
查看次数

有没有办法从Ruby中的实例调用私有Class方法?

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)

ruby visibility scope access-specifier class-method

15
推荐指数
2
解决办法
1万
查看次数

Ruby模块并扩展自我

在什么样的情况下代码:

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,对吧?我很好奇是否有人可以说明何时使用一个代码而不是另一个代码.谢谢!

ruby module class-method instance-methods

15
推荐指数
2
解决办法
3426
查看次数

可以继承python @classmethod吗?

例如,我有一个基类和一个派生类:

>>> 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的类方法?我只想在必要时覆盖类方法.

python inheritance class-method

15
推荐指数
2
解决办法
2万
查看次数

为什么__getitem__不能成为classmethod?

假设以下课程:

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__

python class-method

15
推荐指数
2
解决办法
4313
查看次数

如何在Python中初始化类(而不是实例)?

我想只在加载一个类(而不是每个对象!)时合并当前和继承类的约束.

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方法或者必须使用额外的类装饰器.

任何想法如何初始化类?

python oop inheritance class-method

14
推荐指数
2
解决办法
7132
查看次数