How could a class inherit from another class?
The child
needs to take all of the arguments for the parents
, and pass them along. You don't generally pass a superclass instance to the subclass; that's composition, not inheritance.
class child(parents):
def __init__(self, dim_x, dim_y, nameprefix,
sequence_number, image_path='/vol/'):
super(child, self).__init__(dim_x, dim_y, nameprefix,
sequence_number)
self.IMG_PATH = image_path
...
This is then called:
c = child(300, 300, 'Test', 0)
You don't need to create a parents
instance, create the child
directly.
Note that, per the style guide, the class names should really be Parent
and Child
.
How do you make a css class inherit all values from another class without changing the original class
There is no way to inherit all values from another class in pure CSS.
CSS inheritance is from parent element to child element, this doesn't apply for rulesets such as CSS classes.
We can achieve this, with CSS preprocessors such as Sass & LESS, by extending the classes:
example in Sass (with SCSS syntax):
.block {
display: block
}
.green {
background: green
}
.green-block {
@extend .block; // in LESS, the syntax would be the same but without @extend
@extend .green; // in LESS, the syntax would be the same but without @extend
}
I would just use those CSS classes but override with the new CSS classes all the styles that you need to override.
If you need to inherit all the styles from the CSS class, just use the same CSS class twice and, if necessary, create a new class to override the styles that you don't need.
Does a class that inherits another class inherit that classes inherited classes/interfaces?
The answer is "Yes" -- interfaces are inherited. However, whether you can omit including interface definitions from a class depends on whether that class wants to have certain interface members as explicit interface implementations.
Let's take this very simple (and silly) example:
public interface IMyInterface
{
void foo();
}
public class A : IMyInterface
{
public void foo()
{
Console.Out.WriteLine("A.foo() executed");
}
}
public class B : A, IMyInterface
{
void IMyInterface.foo()
{
Console.Out.WriteLine("B.foo() executed");
}
}
Since B wants to have the foo() method as an explicit interface implementation, the IMyInterface interface has to be specified as part of its class definition -- even if class A already implements this interface. Inheriting an interface declaration from a base class is not sufficient in this case.
If class B would not have explicit interface implementations, then class B does not need to specify the interface (IMyInterface in this little example) again.
Inheriting an interface from a base class and having some explicit interface implementation for members of that interface can lead to effects which can be surprising at best, and at worst can lead to broken/buggy software.
If you execute the following code sequence:
A obj = new A();
obj.foo();
IMyInterface x = obj;
x.foo();
the output will be
A.foo() executed
A.foo() executed
However, let's now use an object of type B but otherwise keep the code exactly the same:
B obj = new B();
obj.foo();
IMyInterface x = obj;
x.foo();
the output will be somewhat different:
A.foo() executed
B.foo() executed
Why is that so? Remember that class B inherits the implementation of the foo() method from class A. Thus, calling obj.foo()
will still execute the inherited foo() method.
Now, why is then x.foo()
not invoking the foo() implementation provided by class A as well? Or, why is obj.foo()
not invoking the implementation given for foo() in class B?
Because x is a variable of type IMyInterface, thus the explicit interface implementation of the foo() method provided by the object of type B takes precedence when invoking x.foo()
. The variable obj is not of type IMyInterface, hence obj.foo()
will not invoke the explicit interface implementation of the foo() method.
Because of these surprising results it is easy to understand why explicit interface implementations for interfaces which are already implemented by a base class is really a bad idea in most cases. As the saying goes: Just because you can does not mean you should.
How to inherit from ABC and another class
You can simply use
class B(A):
pass
or any other code that is common to all children, but without m
. This will still be abstract due to the absence of m
.
An alternative is to use
class B(A, ABC):
pass
In my opinion this is less good. This states that B
is both an A
and an ABC
, while the former alternative states that B
is an A
, and it is an ABC
insofar as much as A
is an ABC
itself.
Consider the case where you decide later on that A
is not an ABC
after all. For M1
and M2
, no change needs to be made. For B
, you now need to remove its being an ABC
as well. While making sure that A
and B
are synchronized in subclassing ABC
is not a huge overhead in terms of code maintenance, I think it does indicate that the design is problematic, PyCharm's warning about the former alternative notwithstanding (I would suppress it as a false-positive).
Can a CSS class inherit one or more other classes?
There are tools like LESS, which allow you to compose CSS at a higher level of abstraction similar to what you describe.
Less calls these "Mixins"
Instead of
/* CSS */
#header {
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
border-radius: 8px;
}
#footer {
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
border-radius: 8px;
}
You could say
/* LESS */
.rounded_corners {
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
border-radius: 8px;
}
#header {
.rounded_corners;
}
#footer {
.rounded_corners;
}
How to inherit a variable from another class
do_num()
needs to setwrite.c
rather thanself.c
so that it sets the class attribute rather than an instance attribute. And you need to callno_num()
.
class write:
c = 0
a = read.get_num()
def do_num(self):
b = read.get_num()
write.c = 10
return b
w = write()
w.do_num()
print(write.c)
- You need to define the classes on the other order. You can't access
write.c
before the class is defined.
class write:
c = 0
a = read.get_num()
def do_num(self):
b = read.get_num()
write.c = 10
return b
class read:
a = 0
c = write.c
def get_num():
a = 6
return a
- You need to make
read.c
a class property that reads the value ofwrite.c
whenever it's used. See How to make a class property? for how to define a class property.
Related Topics
Chrome Dev Tools Rgba/Hsl Conversion to Some New Format
Svg <Mask> Tag Required for Firefox But Appears to Break CSS Mask in Chrome
Jquery Mobile Page Transition Without Jquery Mobile
Adding Class to Editorfor in MVC
Create Clip-Path Wave CSS Edges
How to Disable the "Responsive" Grid in Bootstrap
Variable Sized Column with Ellipsis in a Table
:Hover Not Working on Svg When Svg Is in External File
Target First Letter of Each Word in CSS
Stop Animation from Replaying When Parent Switches from Display:None to Block
CSS Animation on Hover Stay at Last Keyframe When Using Transform: Rotate
Border-Radius with Border-Image
How to Make Chosen Select Go Over Parent Div
CSS Transform + CSS Transition = Skipped Frames [Google Chrome]