Comments on: Python factory-like type instances http://eikke.com/python-factory-like-type-instances/ 'cause this is what I do Tue, 04 Dec 2012 00:03:23 +0000 hourly 1 http://wordpress.org/?v=3.4.1 By: Pythongg http://eikke.com/python-factory-like-type-instances/comment-page-1/#comment-48514 Pythongg Mon, 18 Apr 2011 00:32:08 +0000 http://eikke.com/python-factory-like-type-instances/#comment-48514 So if you were to do what you said but instead had WindowsFoo and Linux foo be in two different files, how could you achieve the same result? __subclasses__() is empty if WindowsFoo and LinuxFoo are not in the same file I'm asking because I'm trying to implement the same sort of idea but I want my subclasses to be in different files So if you were to do what you said but instead had WindowsFoo and Linux foo be in two different files, how could you achieve the same result? __subclasses__() is empty if WindowsFoo and LinuxFoo are not in the same file

I’m asking because I’m trying to implement the same sort of idea but I want my subclasses to be in different files

]]>
By: Quikee http://eikke.com/python-factory-like-type-instances/comment-page-1/#comment-591 Quikee Mon, 11 Feb 2008 20:25:00 +0000 http://eikke.com/python-factory-like-type-instances/#comment-591 I don't know.. I prefer the below example mainly because it is without metaclass hackery (Foo magicly has some functionality but does not inherit from anything), but not quite so different: <pre>import sys class FooBase(object): def __new__(cls, *arguments, **keyword): for subclass in FooBase.__subclasses__(): if subclass.platform == sys.platform: return super(cls, subclass).__new__(subclass, *arguments, **keyword) raise Exception, 'Platform not supported' def say_foo(self): print 'foo' def say_platform_foo(self): raise NotImplementedError class WindowsFoo(FooBase): platform = 'win32' def say_platform_foo(self): print 'win32 foo' class LinuxFoo(FooBase): platform = 'linux2' def say_platform_foo(self): print 'linux foo' if __name__ == '__main__': foo = FooBase() foo.say_platform_foo() def say_platform_foo(self): print 'linux foo'</pre> I don’t know.. I prefer the below example mainly because it is without metaclass hackery (Foo magicly has some functionality but does not inherit from anything), but not quite so different:

import sys

class FooBase(object):
	def __new__(cls, *arguments, **keyword):
		for subclass in FooBase.__subclasses__():
			if subclass.platform == sys.platform:
				return super(cls, subclass).__new__(subclass, *arguments, **keyword)
		raise Exception, 'Platform not supported'
	
	def say_foo(self):
		print 'foo'

	def say_platform_foo(self):
		raise NotImplementedError

class WindowsFoo(FooBase):
	platform = 'win32'
	
	def say_platform_foo(self):
		print 'win32 foo'

class LinuxFoo(FooBase):
	platform = 'linux2'
	
	def say_platform_foo(self):
		print 'linux foo'

if __name__ == '__main__':
	foo = FooBase()
	foo.say_platform_foo()
	def say_platform_foo(self):
		print 'linux foo'
]]>