from itertools import count
class myobject(object):
id=count(0)
def __init__(self):
self.ID=next(self.id)
self.dat=[]
class bucket(object):
def __init__(self):
self.container_object=self.Container_object
class Container_object(object):
self.container={}
self.contained_objects=self.container.keys()
def create_object(myobject):
self.container[myobject.ID]=object_data
I am looking to create a container object within class bucket such that I can create different instances of myobject within class bucket.
Such that when create_object creates new object that can be accessed and contained within class bucket.Container_object and bucket.Container_object.ID would be an instance of myobject
PS I am new to classes and still try to understand how I can use them, so most likely the question shows this understanding. Please feel free to point to those misunderstandings. thanks
I think this is what you are looking for.
A class Bucket which contains multiple instances of class MyObject. Since you don't specify any particular peculiarities of your Container object, I will use a simple dictionary.
from itertools import count
class MyObject(object):
id=count(0)
def __init__(self):
self.ID=next(self.id)
self.dat=[]
class Bucket(object):
def __init__(self):
self.contained_objects={}
def add_object(myobject):
self.contained_objects[myobject.ID]=myobject.dat
If you want to wrap the collection inside some other object for some reasons, you simply define the WrapperContainer class outside the Bucket class and simply instantiate an instance of WrapperContainer inside it.
from itertools import count
class MyObject(object):
id=count(0)
def __init__(self):
self.ID=next(self.id)
self.dat=[]
class WrapperContainer(object):
def __init__(self):
self.collection = {}
def add_object(my_object):
self.collection[myobject.ID]=myobject.dat
def do_whatever_you_want():
pass
class Bucket(object):
def __init__(self):
self.container_object= WrapperContainer() #a specific instance
def add_object(myobject):
self.container_object.add_object(my_object)
Finally, you can think at your container class as a subclass of an already existent collection (list or dictionary or anything else) to inherit its features.
Related
I wrote a Python module, with several classes that inherit from a single class called MasterBlock.
I want to import this module in a script, create several instances of these classes, and then get a list of all the existing instances of all the childrens of this MasterBlock class. I found some solutions with vars()['Blocks.MasterBlock'].__subclasses__() but as the instances I have are child of child of MasterBlock, it doesn't work.
Here is some example code:
Module:
Class MasterBlock:
def main(self):
pass
Class RandomA(MasterBlock):
def __init__(self):
pass
# inherit the main function
Class AnotherRandom(MasterBlock):
def __init__(self):
pass
# inherit the main function
Script:
import module
a=module.RandomA()
b=module.AnotherRandom()
c=module.AnotherRandom()
# here I need to get list_of_instances=[a,b,c]
Th ultimate goal is to be able to do:
for instance in list_of_instances:
instance.main()
If you add a __new__() method as shown below to your base class which keeps track of all instances created in a class variable, you could make the process more-or-less automatic and not have to remember to call something in the __init__() of each subclass.
class MasterBlock(object):
instances = []
def __new__(cls, *args, **kwargs):
instance = super(MasterBlock, cls).__new__(cls, *args, **kwargs)
instance.instances.append(instance)
return instance
def main(self):
print('in main of', self.__class__.__name__) # for testing purposes
class RandomA(MasterBlock):
def __init__(self):
pass
# inherit the main function
class AnotherRandom(RandomA): # works for sub-subclasses, too
def __init__(self):
pass
# inherit the main function
a=RandomA()
b=AnotherRandom()
c=AnotherRandom()
for instance in MasterBlock.instances:
instance.main()
Output:
in main of RandomA
in main of AnotherRandom
in main of AnotherRandom
What about adding a class variable, that contains all the instances of MasterBlock? You can record them with:
Class MasterBlock(object):
all_instances = [] # All instances of MasterBlock
def __init__(self,…):
…
self.all_instances.append(self) # Not added if an exception is raised before
You get all the instances of MasterBlock with MasterBlock.all_instances (or instance.all_instances).
This works if all base classes call the __init__ of the master class (either implicitly through inheritance or explicitly through the usual super() call).
Here's a way of doing that using a class variable:
class MasterBlock(object):
instances = []
def __init__(self):
self.instances.append(self)
def main(self):
print "I am", self
class RandomA(MasterBlock):
def __init__(self):
super(RandomA, self).__init__()
# other init...
class AnotherRandom(MasterBlock):
def __init__(self):
super(AnotherRandom, self).__init__()
# other init...
a = RandomA()
b = AnotherRandom()
c = AnotherRandom()
# here I need to get list_of_instances=[a,b,c]
for instance in MasterBlock.instances:
instance.main()
(you can make it simpler if you don't need __init__ in the subclasses)
output:
I am <__main__.RandomA object at 0x7faa46683610>
I am <__main__.AnotherRandom object at 0x7faa46683650>
I am <__main__.AnotherRandom object at 0x7faa46683690>
I am working on a code base that uses oop and I am relavtively new to it. My question specifically is, why NewMenuItem not inherit from File?
code bunk to play with code: https://codebunk.com/b/350127244/
"""Build class hierarchy and get values from decendants"""
import inspect
def selftest():
class Menu(object):
def __init__(self):
super(Menu, self).__init__()
self.value = "Menu"
class MenuBar(Menu):
#having object in there makes it a new style object, which allows us to use super
def __init__(self):
super(MenuBar, self).__init__()
self.value = "MenuBar"
class File(MenuBar):
def __init__(self):
Menu.__init__()
super(File, self).__init__()
self.value = "File"
self.FileValue = "File here!"
class New(Menu):
def __init__(self):
Menu.__init__()
pass
class NewMenuItem(Menu):
def __init__(self):
"""
Q 1- Why do I need self here?
Menu.__init__(self)
"""
Menu.__init__(self)
pass
def show_vals(self):
print(self.value)
"""
Q 2 -why wont this work?
def show_vals2(self):
print(self.FileValue)
"""
example = File.New.NewMenuItem()
example.show_vals()
"""
Q 3 - Why do I get this error with this line?
inspect.getmro(example)
AttributeError: 'ManageProduct' object has no attribute '__bases__'
"""
I'm trying to understand what is happening line by line, but what I don't get is why NewMenuItem doesn't inherit from File.
I tried hard-coding the instantiation of File,like so:
File.init()
but then I get an error unless I pass the File object:
File.__init__(File())
I guess what I am struggling with is:
-inheritance trees
-super classes
-why we need to hard-code instantiations in this case
Keep in mind that this is the code I have come across. I am not sure why this is the way it is.
Inheritance and scope are two completely different things. NewMenuItem is defined inside the scope of the class New, inside of the scope of the class File, but it inherits from Menu, which inherits from object. So while NewMenuItem will only be accessible through the class File and again through New, it will inherit its methods from Menu, and super will refer to Menu.
I am using motor with tornado. I have the following class:
class N():
def __init__(self,collectionName host='localhost', port=27017):
self.con=motor.MotorClient(host,port)
self.xDb=self.con.XDb
setattr(self,collectionName,self.xDb[collectionName])
This is actually a parent class that i want to extend. The child class will call this class' init to set the collectionName. The problem is I also have some other methods in this class E.g.
#tornado.gen.coroutine
def dropDB(self):
yield self.xDb.drop_collection(self.COLLECTION??)
The above is broken because I dynamically set the collection in the init what's a way I can determine the self. variable I set to use in the base methods?
Set another variable:
class N():
def __init__(self, collectionName, host='localhost', port=27017):
# ... your existing code ...
self.collectionName = collectionName
#tornado.gen.coroutine
def dropDB(self):
yield self.xDb.drop_collection(self.collectionName)
Since drop_collection takes a name or a MotorCollection object, there are other ways you could store this data on self, but the way I showed might be the easiest.
http://motor.readthedocs.io/en/stable/api/motor_database.html#motor.motor_tornado.MotorDatabase.drop_collection
When I create a parent class and child class as shown below, why don't the arguments from the parent class automatically get pulled in by the child class?
I understand that explicit is better, but I'm wondering in what circumstance this code...
class testParent(object):
def __init__(self,testParentParam1,testParentParam2):
pass
class testChild(testParent):
def __init__(self,testParentParam1,testParentParam2,testChildParam1,testChildParam2):
pass
Is better than this code...
class testParent(object):
def __init__(self,testParentParam1,testParentParam2):
pass
class testChild(testParent):
def __init__(self,testChildParam1,testChildParam2):
pass
Derived classes extend base classes. That means they might need more/less/different information at construction time to do their extending. Consider:
class BaseTextDocument(object):
def __init__(self, content):
self.content = content
class WordDocument(object):
def __init__(self, path, word_version="guess_from_file"):
content = parse_word_document(path, word_version)
super(WordDocument, self).__init__(content)
I'm trying to provide framework which allows people to write their own plugins. These plugins are basically derived classes. My base class needs some variables to initialize, how can I initialize my base class without having to let my derived class feed the variable in the base class initialization?
#!/bin/python
class BaseClass():
def __init__(self,config):
self.config=config
def showConfig(self):
print "I am using %s" % self.config
class UserPlugin(BaseClass):
def __init__(self,config):
BaseClass.__init__(self,config)
def doSomething(self):
print "Something"
fubar = UserPlugin('/tmp/config.cfg')
fubar.showConfig()
My goal is to avoid the need to define the config parameter in the UserPlugin class, since this is something I don't want the user who writes a plugin to be bothered with.
You can use argument lists to pass any remaining arguments to the base class:
class UserPlugin(BaseClass):
def __init__(self, *args, **kwargs):
BaseClass.__init__(self, *args, **kwargs)
Based on your Pastebin code, how about this? This avoids using a separate global, instead using a class attribute, which is accessible as a member to all derived classes and their instances.
#!/bin/python
class BaseClass():
config = '/tmp/config.cfg'
def __init__(self):
pass
def showConfig(self):
print "I am using %s" % self.config
class UserPlugin(BaseClass):
def __init__(self):
BaseClass.__init__(self)
def doSomething(self):
print "Something"
fubar = UserPlugin()
fubar.showConfig()
This was the other way to do it that I mentioned before. Keep in mind that if you want to change the value of BaseClass.config itself, you should access it directly (i.e. BaseClass.config = '/foo/path'; otherwise, you wind up creating a custom UPinstance.config value, leaving BaseClass.config unchanged.