Classes at runtime

L

Lethalman

(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?
 
M

mark sparshatt

Lethalman said:
(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
 
G

gabriele renzi

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=> true
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top