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 wish to set the parentNumber individually, and then reach that number via the child class, and in this case use it for some multiplication.
I'm new to the OOP area so any general pointers on inheritance is welcome as well!
More info: I'm designing a project management solution for vfx-based projects, and am playing with classes and inheritance to see how they can help me the most.
Right now, I've got the top class, Project, and a derived class, Shot. Shot has a self.length variable with the length of the specific shot. It's also got a getLengthInSeconds() method that uses self.length along with the Project.fps to determine the length in seconds. Project has a setFps() method in which the fps is set after an instance of the class is created.
I'm kind of used to variables being prefixed with self. and have not experimented much with classes using the more "global" variables without self. . If I make everything global, no self., I can use Project.fps without hassle, but I'm getting a "bad programming practice" warning in my neckhair. Perhaps there is a better, more neat, way?
Edit:
After some reading, super() seems kinda dangerous, and a bit more than I need I think. I am mainly having single-inheritance classes and aren't even sure how to make use of diamond hierarchies.. Is there a safer way to access superclass variables and methods that doesn't include super()?
Edit:
Allright, see if this makes sense or if I'm thinking about it all wrong.
I'm looking at classes and inheritance as groups and children. A child knows it's parent and all it's values. A child to another parent knows That parents values. What I'm trying to accomplish is having all shots created be part of a project. And right now, I'm creating Shot() instances from within Project() class, adding the instances to a list of shots which is then maintained within 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 sort of see the mis-steps in logic here, but no real way around it.. Seems like every child is getting a new instance of the parent this way, where parent_id is always an empty string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With