"Eric Sandeen" <eric_sandeen.NO at spam.bigfoot.com> wrote in message news:9chp0n$sk2$1 at news.jump.net... > 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... It ain't silly, and it would be no problem if it were... > I'm trying to create a data structure which holds information about > several different filesystems - for example: ext2, reiserfs, and xfs. I'd > like this to be easily extensible to add other filesystems, as well. The > type of information I'm including is name, magic number, position of magic > number in superblock, etc. So far, I'm doing something like this (these > values aren't right, just an example) : > > 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 So far, so good. Presumably, then, you have some class such as: class FileSystem: pass or maybe it has some extra attributes, but this isn't mandatory (it's OK to add attributes per-instance). Still, it might handier to have: class FileSystem: def __init__(self, prettyName, magicNo, magicNoPos): self.prettyName = prettyName self.magicNo = magicNo self.magicNoPos = magicNoPos so that FSystems['ext2'] = FileSystem("ext2 filesystem", 0xCE32, 1080) and so on, will suffice. Strictly an issue of more convenience for you when you're filling the FSystems dictionary. > This way I can iterate over FSystems.keys() to do things like check for a > filesystem, create menus, etc. These activities are basically the same > regardless of the filesystem, so they're easy to handle in a single > function. But if some sub-activities differ in detail by filesystem, you may still use a common function for the overall structure and call the appropriate object methods for whatever. It's called the 'Template Design Pattern' and it's pretty important. Anyway...: > 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() > > In C, I'd have a pointer to the function I want. Is something like this > possible in Python? Just about everything in Python is accessed through references, which act much like pointers. Specifically, you can do, for example: def specific_ext2_creator(): # whatever you want here and then FSystems['ext2'].unique_fs_creator = specific_ext2_creator and you're all set to call the .unique_fs_creator() on this entry of your dictionary. Note that, like in C, just MENTIONING the function doesn't call it but rather yields/lets you set a reference; it's the presence of _parentheses_ () after the function name or other reference to it that let you actually CALL the thing. In C you may also indicate the referencing or dereferencing via the unary operators & and *, redundantly in the case of functions, but in Python "any mention is a reference" for functions just like for any other object. There are other approaches -- you may define a _subclass_ of FileSystem and use methods rather than function, and this is often handier, as the instance on which the method is being called will be always available as the first argument. But if you do want to work with functions rather than with classes and methods, you may do that too -- it's up to you, Python does support both paradigms well! Alex
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