Hope this is clear enough:
class myParent(): def __init__( self ): self.parentNumber = 5 class Child( myParent ): def __init__( self ): self.childNumber = 4 def multiplyNumbers( self ): print myParent.parentNumber * self.childNumber p = Child() p.multiplyNumbers()
I want to set the parent number separately, and then reach that number through the child class and in this case use it for some multiplication.
I am new to OOP, so any generic inheritance pointers are also welcome!
Additional Information: I am developing a project management solution for vfx-based projects and playing with classes and inheritance to see how they can help me.
I now have a class, Project, and a Shot derived class. Shot has a self.length variable with the length of a particular frame. He also got the getLengthInSeconds () method, which uses self.length along with Project.fps to determine the length in seconds. The project has a setFps () method in which fps is set after the class is instantiated.
I'm kind of used to variables that have a prefix. and haven’t experimented much with classes using more “global” variables without an “I.” If I do everything global, I don’t know, I can use Project.fps without any hassle, but I get a warning about “bad programming practice” in my unskilled condition. Perhaps there is a better, neater way?
Edit:
After some reading, super () seems like something dangerous, and a little more than I need, I think. I basically have single inheritance classes and am not even sure how to use dialog hierarchies. Is there a safer way to access superclass variables and methods that do not include super ()?
Edit:
Okay, let's see if this makes sense or I think it's all wrong.
I view classes and inheritance as groups and children. A child knows its parent and all its meanings. A child knows to another parent what parents value. What I'm trying to accomplish is that all the snapshots created are part of the project. And right now I'm creating Shot () instances from the Project () class, adding instances to the list of snapshots that are then supported in the Project () instance.
i.e.
class myParent( object ): def __init__( self ): self.parent_id = '' self.children = [] def createChild( self, name ): self.children.append( myChild( name ) ) def getChildren( self ): return self.children def setParentId( self, id ): self.parentId = id class myChild( myParent ): def __init__( self, id ): super(myChild, self).__init__() self.id = id def getParentId( self ): return self.parent_id p = myParent() p.setParentId( 'parent01' ) p.createChild( 'child01' ) print p.getChildren()[0].getParentId()
I can look at the erroneous steps in the logic here, but there is no real way. It seems that every child gets a new instance of the parent in a way where parent_id is always an empty string.