Classes at runtime

(sorry for my poor English)
Hello,
i can make classes at runtime in Python, just like this:

class test: pass
class myclass:
  def make(self, inherit):
    class temp(inherit): pass
    return temp()

instance = myclass().make(test)

instance is now a class which inherits test... this is a really simple example but i need to know because i can't do this in ruby.
So, is python more dynamic with objects? Isn't ruby good for this kind of meta programming?

Lethalman wrote:

(sorry for my poor English)
Hello,
i can make classes at runtime in Python, just like this:

class test: pass
class myclass:
    def make(self, inherit):
        class temp(inherit): pass
        return temp()

instance = myclass().make(test)

instance is now a class which inherits test... this is a really simple example but i need to know because i can't do this in ruby.
So, is python more dynamic with objects? Isn't ruby good for this kind of meta programming?

Try this

class Test
end

klass = Class.new(Test)
p klass.ancestors #=> [#<Class:.....>, Test, Object, Kernel]

klass is a subclass of Test.

HTH

···

--
Mark Sparshatt

mark sparshatt ha scritto:

So, is python more dynamic with objects? Isn't ruby good for this kind of meta programming?

Try this

class Test
end

klass = Class.new(Test)
p klass.ancestors #=> [#<Class:.....>, Test, Object, Kernel]

klass is a subclass of Test.

HTH

... and consider you can normally just use a mixin in ruby
>> module Foo
>> def foo
>> 'mixins rule!'
>> end
>> end
=> nil
>> a='hello'
=> "hello"
>> a.extend Foo
=> "hello"
>> a.foo
=> "mixins rule!"
>> a.is_a? Foo
=> true