Eric Sandeen wrote: > First a disclaimer - I'm pretty new to Python, and OOP is not really my > bag, so forgive me if this is a silly question... OOP is easy. Its just data and functions. > I'm trying to create a data structure which holds information about > several different filesystems [...] > FSystems['ext2'].PrettyName = "ext2 filesystem" > FSystems['ext2'].MagicNo = 0xce32 > FSystems['ext2'].MagicNoPos = 1080 > > FSystems['xfs'].PrettyName = "XFS filesystem" > FSystems['xfs'].MagicNo = "XFSB" > FSystems['xfs'].MagicNoPos = 0 [...] > The problem comes when I'd like to define a unique function for each > filesystem to actually create the filesystem, since this will vary quite a > bit from fs to fs. I'd like to access it via something like > > FSystems[<fstype>].unique_fs_function() Funtions are first class objects in Python. You can use them just like any other objects (like and integer or string). This will work: def ext2_fs_function(): ... FSystems["ext2"].unique_fs_function = ext2_fs_function A more OO approach would be to use a class: FSystems = {} def register_fs(klass): FSystems[klass.name] = klass class FileSystem: def create(self): raise NotImplementedError class Ext2FileSystem(FileSystem): name = "ext2" pretty_name = "ext2 filesystem" ... def __init__(self, device, mount_point): self.device = device self.mount_point = mount_point def create(self): ... register_fs(Ext2FileSystem) def make_fs(type, *args): klass = FSystems[type] return apply(klass, args) home = make_fs("ext2", "/dev/hda1", "/home") home.create() Cheers, Neil
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4