0

I have a class definition

class DataItem(object):
    """Elementary piece of data."""

I want to define several instances of this class and store them somewhere. I expected that I could define diOne and diTwo as members of the class DataItem and store them in diOne.py and diTwo.py somewhere. However, I'd like it such that when I imported the module with DataItem defined, that I'd automatically have access to all the defined instances.

The question is, how do I organize the module and instance files? Am I thinking about this correctly?

Edit: to flesh out my intentions,

I expect there to be a lot more code that defines each instance. I want them in separate files so I can have better/easier version control over each instance as they will have lives of their own. I could easily define them in the same module but changing the module and controlling who has permission to change that code seems a much bigger hassle when a potential of 100 plus instances could be defined. Each with varying degree of. Hangs frequency.

I hope that clarifies.

6
  • 2
    What are you hoping to accomplish by doing this? It doesn't sounds like you are thinking about it correctly, but the question is rather vague. Having an instance in a file by itself, in particular, is not something that makes much sense. Commented Sep 23, 2013 at 18:11
  • 1
    Also, normally instances don't have "a lot of code". It may take some code to generate the appropriate arguments to pass to the constructor, if that's what you mean… but if you're trying to attach different methods to each instance, what you probably want is two subclasses of the class (and an instance of each), not two instances of the class. Commented Sep 23, 2013 at 18:52
  • More generally, instead of showing us a sketchy, abstract example, come up with a concrete example of what you want the class and its instances to do (complex enough to demonstrate all of the issues, but no more). Commented Sep 23, 2013 at 18:54
  • @abarnert, I was trying to clarify my question further realizing your comment was spot on. But just to be clear, you're suggesting that what I was intending to be instances should just be subclasses with one instance each? Considering that, should all those subclasses exist in separate files in a sub directory? How do I import them all? Commented Sep 23, 2013 at 20:03
  • @piRSquared: Without knowing what you meant by "a lot more code that defines each instance", I can't say yes or not to that. Do you mean a lot of code to generate the arguments to pass to each constructor, or do you mean a lot of separate methods in each instance with unique code, or something totally different? Commented Sep 23, 2013 at 20:14

2 Answers 2

2

I believe you can just do something like below. Make sure they are in the same directory. You can then create as many instances as you want in Instances.py (or have multiple files with the same structure). Does that answer your question?

MyClass.py

class DataItem(object):
    def __init__(self, v):
        self.value = v

Instances.py

import MyClass

# Creates an instance of DataItem, and prints its value
diOne = MyClass.DataItem(1)
print diOne.value
Sign up to request clarification or add additional context in comments.

4 Comments

Why would you create a class attribute value just to hide it with an instance attribute of the same name in the __init__ method?
Instead of adding a comment saying "this is unnecessary", just remove the line from your code. Also, you don't need to append "Edit:" stuff to the end. It's more important for an answer to say exactly what it should to be helpful than to show the history of the answer, especially since SO lets you see the revisions.
Sorry, I wasn't sure on the etiquette for editing to incorporate other user's comments. I just wanted to make sure to give credit where due.
If you're really worried about credit, you can always upvote the comment. But really, it's not that important; making the answer useful to the OP and future readers matters a lot more than making sure rep points are allocated fairly.
1

There's no reason to have separate diOne/diTwo files - they would be almost empty. If you really needed to do this, you can just do it in the file that defines the class:

data_item.py:

class DataItem(object):
    pass

di_one = DataItem()
di_two = DataItem()

Now when you do import data_item anywhere, you will automatically have references to data_item.di_one and data_item.di_two - or, you can do from data_item import di_one, di_two to reference them directly.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.