Retrieve all functions and attributes from an already instanced parent in the child class
I'm trying to do this : Initialise child class with instance of parent class.
genomic_feature_instance is instanced and contains what follows :
{'name': 'ENSG00000223972.5', 'type': 'gene', 'iv': , 'source': 'HAVANA',
'score': '.', 'frame': '.', 'attr' : {'ID': 'ENSG00000223972.5',
'gene_id': 'ENSG00000223972.5', 'gene_type':
'transcribed_unprocessed_pseudogene', 'gene_status': 'KNOWN',
'gene_name': 'DDX11L1', 'leve l': '2', 'havana_gene':
'OTTHUMG00000000961.2'}}
I want to create a custom/child instance to manipulate attributes and functions from this class of genomic_feature_instance.
aCustomGenomicFeature = CustomGenomicFeature(genomic_feature_instance,'kikou')
This is what I have done but I would like to use super() to inherit all attribute values from this genomic_feature (instance of GenomicFeature)
class CustomGenomicFeature(GenomicFeature):
def __init__(self,genomic_feature_instance,kikou):
# call parent constructor to set name and color
self.genomic_feature = genomic_feature_instance
# Subclass-specific stuff follows
self.kikou=kikou
python-3.x inheritance
add a comment |
I'm trying to do this : Initialise child class with instance of parent class.
genomic_feature_instance is instanced and contains what follows :
{'name': 'ENSG00000223972.5', 'type': 'gene', 'iv': , 'source': 'HAVANA',
'score': '.', 'frame': '.', 'attr' : {'ID': 'ENSG00000223972.5',
'gene_id': 'ENSG00000223972.5', 'gene_type':
'transcribed_unprocessed_pseudogene', 'gene_status': 'KNOWN',
'gene_name': 'DDX11L1', 'leve l': '2', 'havana_gene':
'OTTHUMG00000000961.2'}}
I want to create a custom/child instance to manipulate attributes and functions from this class of genomic_feature_instance.
aCustomGenomicFeature = CustomGenomicFeature(genomic_feature_instance,'kikou')
This is what I have done but I would like to use super() to inherit all attribute values from this genomic_feature (instance of GenomicFeature)
class CustomGenomicFeature(GenomicFeature):
def __init__(self,genomic_feature_instance,kikou):
# call parent constructor to set name and color
self.genomic_feature = genomic_feature_instance
# Subclass-specific stuff follows
self.kikou=kikou
python-3.x inheritance
add a comment |
I'm trying to do this : Initialise child class with instance of parent class.
genomic_feature_instance is instanced and contains what follows :
{'name': 'ENSG00000223972.5', 'type': 'gene', 'iv': , 'source': 'HAVANA',
'score': '.', 'frame': '.', 'attr' : {'ID': 'ENSG00000223972.5',
'gene_id': 'ENSG00000223972.5', 'gene_type':
'transcribed_unprocessed_pseudogene', 'gene_status': 'KNOWN',
'gene_name': 'DDX11L1', 'leve l': '2', 'havana_gene':
'OTTHUMG00000000961.2'}}
I want to create a custom/child instance to manipulate attributes and functions from this class of genomic_feature_instance.
aCustomGenomicFeature = CustomGenomicFeature(genomic_feature_instance,'kikou')
This is what I have done but I would like to use super() to inherit all attribute values from this genomic_feature (instance of GenomicFeature)
class CustomGenomicFeature(GenomicFeature):
def __init__(self,genomic_feature_instance,kikou):
# call parent constructor to set name and color
self.genomic_feature = genomic_feature_instance
# Subclass-specific stuff follows
self.kikou=kikou
python-3.x inheritance
I'm trying to do this : Initialise child class with instance of parent class.
genomic_feature_instance is instanced and contains what follows :
{'name': 'ENSG00000223972.5', 'type': 'gene', 'iv': , 'source': 'HAVANA',
'score': '.', 'frame': '.', 'attr' : {'ID': 'ENSG00000223972.5',
'gene_id': 'ENSG00000223972.5', 'gene_type':
'transcribed_unprocessed_pseudogene', 'gene_status': 'KNOWN',
'gene_name': 'DDX11L1', 'leve l': '2', 'havana_gene':
'OTTHUMG00000000961.2'}}
I want to create a custom/child instance to manipulate attributes and functions from this class of genomic_feature_instance.
aCustomGenomicFeature = CustomGenomicFeature(genomic_feature_instance,'kikou')
This is what I have done but I would like to use super() to inherit all attribute values from this genomic_feature (instance of GenomicFeature)
class CustomGenomicFeature(GenomicFeature):
def __init__(self,genomic_feature_instance,kikou):
# call parent constructor to set name and color
self.genomic_feature = genomic_feature_instance
# Subclass-specific stuff follows
self.kikou=kikou
python-3.x inheritance
python-3.x inheritance
asked Jan 18 at 16:24
ZheFrenchZheFrench
51821537
51821537
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Within the init() method of the CustomGenomicFeature class you can explicitly invoke the init() method of the GenomicFeature class by just calling super().init()
class CustomGenomicFeature(GenomicFeature):
def __init__(self,genomic_feature,kikou):
super().__init__(genomic_feature)
self.kikou=kikou
add a comment |
With __dict__.update
I would rather advice the use of __dict__
instead of super
, because super won't help you to copy the value of the attributes of the parent object. So you could do something like that:
class A():
def __init__(self):
self.x = 1
class B(A):
def __init__(self, a):
super(B, self).__init__()
self.__dict__.update(a.__dict__)
a = A()
b = B(a)
print(b.x) # displays '1'
a.x = 3
b2 = B(a)
print(b2.x) # displays '3'. Without the __dict__update, it would return '1'
That way you set every attribute your parent class has without modifying the attributes of the child class that the parent does not have.
In your case:
class CustomGenomicFeature(GenomicFeature):
def __init__(self,genomic_feature_instance,kikou):
self.__dict__.update(genomic_feature_instance.__dict__)
# Subclass-specific stuff follows
self.kikou=kikou
With Composition
Another way (and probably more clean), would be to use composition instead of inheritance:
class CustomGenomicFeature(GenomicFeature):
def __init__(self,genomic_feature_instance,kikou):
# keep ref to the genomic_feature_instance
self._genomic_feature = genomic_feature_instance
# Subclass-specific stuff follows
self.kikou=kikou
@property
def name(self):
return self._genomic_feature.name
More:
- Inheritance vs. Composition
Ok but then you need to define all properties from the parent class then...well ok but i you have many attributes that can be long and I also want to keep function from parent, so inheritance was not so bad idea , no?
– ZheFrench
14 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54257853%2fretrieve-all-functions-and-attributes-from-an-already-instanced-parent-in-the-ch%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Within the init() method of the CustomGenomicFeature class you can explicitly invoke the init() method of the GenomicFeature class by just calling super().init()
class CustomGenomicFeature(GenomicFeature):
def __init__(self,genomic_feature,kikou):
super().__init__(genomic_feature)
self.kikou=kikou
add a comment |
Within the init() method of the CustomGenomicFeature class you can explicitly invoke the init() method of the GenomicFeature class by just calling super().init()
class CustomGenomicFeature(GenomicFeature):
def __init__(self,genomic_feature,kikou):
super().__init__(genomic_feature)
self.kikou=kikou
add a comment |
Within the init() method of the CustomGenomicFeature class you can explicitly invoke the init() method of the GenomicFeature class by just calling super().init()
class CustomGenomicFeature(GenomicFeature):
def __init__(self,genomic_feature,kikou):
super().__init__(genomic_feature)
self.kikou=kikou
Within the init() method of the CustomGenomicFeature class you can explicitly invoke the init() method of the GenomicFeature class by just calling super().init()
class CustomGenomicFeature(GenomicFeature):
def __init__(self,genomic_feature,kikou):
super().__init__(genomic_feature)
self.kikou=kikou
answered Jan 18 at 17:00
BMac413BMac413
464
464
add a comment |
add a comment |
With __dict__.update
I would rather advice the use of __dict__
instead of super
, because super won't help you to copy the value of the attributes of the parent object. So you could do something like that:
class A():
def __init__(self):
self.x = 1
class B(A):
def __init__(self, a):
super(B, self).__init__()
self.__dict__.update(a.__dict__)
a = A()
b = B(a)
print(b.x) # displays '1'
a.x = 3
b2 = B(a)
print(b2.x) # displays '3'. Without the __dict__update, it would return '1'
That way you set every attribute your parent class has without modifying the attributes of the child class that the parent does not have.
In your case:
class CustomGenomicFeature(GenomicFeature):
def __init__(self,genomic_feature_instance,kikou):
self.__dict__.update(genomic_feature_instance.__dict__)
# Subclass-specific stuff follows
self.kikou=kikou
With Composition
Another way (and probably more clean), would be to use composition instead of inheritance:
class CustomGenomicFeature(GenomicFeature):
def __init__(self,genomic_feature_instance,kikou):
# keep ref to the genomic_feature_instance
self._genomic_feature = genomic_feature_instance
# Subclass-specific stuff follows
self.kikou=kikou
@property
def name(self):
return self._genomic_feature.name
More:
- Inheritance vs. Composition
Ok but then you need to define all properties from the parent class then...well ok but i you have many attributes that can be long and I also want to keep function from parent, so inheritance was not so bad idea , no?
– ZheFrench
14 hours ago
add a comment |
With __dict__.update
I would rather advice the use of __dict__
instead of super
, because super won't help you to copy the value of the attributes of the parent object. So you could do something like that:
class A():
def __init__(self):
self.x = 1
class B(A):
def __init__(self, a):
super(B, self).__init__()
self.__dict__.update(a.__dict__)
a = A()
b = B(a)
print(b.x) # displays '1'
a.x = 3
b2 = B(a)
print(b2.x) # displays '3'. Without the __dict__update, it would return '1'
That way you set every attribute your parent class has without modifying the attributes of the child class that the parent does not have.
In your case:
class CustomGenomicFeature(GenomicFeature):
def __init__(self,genomic_feature_instance,kikou):
self.__dict__.update(genomic_feature_instance.__dict__)
# Subclass-specific stuff follows
self.kikou=kikou
With Composition
Another way (and probably more clean), would be to use composition instead of inheritance:
class CustomGenomicFeature(GenomicFeature):
def __init__(self,genomic_feature_instance,kikou):
# keep ref to the genomic_feature_instance
self._genomic_feature = genomic_feature_instance
# Subclass-specific stuff follows
self.kikou=kikou
@property
def name(self):
return self._genomic_feature.name
More:
- Inheritance vs. Composition
Ok but then you need to define all properties from the parent class then...well ok but i you have many attributes that can be long and I also want to keep function from parent, so inheritance was not so bad idea , no?
– ZheFrench
14 hours ago
add a comment |
With __dict__.update
I would rather advice the use of __dict__
instead of super
, because super won't help you to copy the value of the attributes of the parent object. So you could do something like that:
class A():
def __init__(self):
self.x = 1
class B(A):
def __init__(self, a):
super(B, self).__init__()
self.__dict__.update(a.__dict__)
a = A()
b = B(a)
print(b.x) # displays '1'
a.x = 3
b2 = B(a)
print(b2.x) # displays '3'. Without the __dict__update, it would return '1'
That way you set every attribute your parent class has without modifying the attributes of the child class that the parent does not have.
In your case:
class CustomGenomicFeature(GenomicFeature):
def __init__(self,genomic_feature_instance,kikou):
self.__dict__.update(genomic_feature_instance.__dict__)
# Subclass-specific stuff follows
self.kikou=kikou
With Composition
Another way (and probably more clean), would be to use composition instead of inheritance:
class CustomGenomicFeature(GenomicFeature):
def __init__(self,genomic_feature_instance,kikou):
# keep ref to the genomic_feature_instance
self._genomic_feature = genomic_feature_instance
# Subclass-specific stuff follows
self.kikou=kikou
@property
def name(self):
return self._genomic_feature.name
More:
- Inheritance vs. Composition
With __dict__.update
I would rather advice the use of __dict__
instead of super
, because super won't help you to copy the value of the attributes of the parent object. So you could do something like that:
class A():
def __init__(self):
self.x = 1
class B(A):
def __init__(self, a):
super(B, self).__init__()
self.__dict__.update(a.__dict__)
a = A()
b = B(a)
print(b.x) # displays '1'
a.x = 3
b2 = B(a)
print(b2.x) # displays '3'. Without the __dict__update, it would return '1'
That way you set every attribute your parent class has without modifying the attributes of the child class that the parent does not have.
In your case:
class CustomGenomicFeature(GenomicFeature):
def __init__(self,genomic_feature_instance,kikou):
self.__dict__.update(genomic_feature_instance.__dict__)
# Subclass-specific stuff follows
self.kikou=kikou
With Composition
Another way (and probably more clean), would be to use composition instead of inheritance:
class CustomGenomicFeature(GenomicFeature):
def __init__(self,genomic_feature_instance,kikou):
# keep ref to the genomic_feature_instance
self._genomic_feature = genomic_feature_instance
# Subclass-specific stuff follows
self.kikou=kikou
@property
def name(self):
return self._genomic_feature.name
More:
- Inheritance vs. Composition
answered Jan 18 at 17:03
olinox14olinox14
35117
35117
Ok but then you need to define all properties from the parent class then...well ok but i you have many attributes that can be long and I also want to keep function from parent, so inheritance was not so bad idea , no?
– ZheFrench
14 hours ago
add a comment |
Ok but then you need to define all properties from the parent class then...well ok but i you have many attributes that can be long and I also want to keep function from parent, so inheritance was not so bad idea , no?
– ZheFrench
14 hours ago
Ok but then you need to define all properties from the parent class then...well ok but i you have many attributes that can be long and I also want to keep function from parent, so inheritance was not so bad idea , no?
– ZheFrench
14 hours ago
Ok but then you need to define all properties from the parent class then...well ok but i you have many attributes that can be long and I also want to keep function from parent, so inheritance was not so bad idea , no?
– ZheFrench
14 hours ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54257853%2fretrieve-all-functions-and-attributes-from-an-already-instanced-parent-in-the-ch%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown