Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am trying to use composition in python but I wasn’t sure how to do it if I needed the __init__ method from the other class. In other words, how do I do this super(childClass, self).__init__(…) with composition?
for example:
class Class*(object):
….
class ClassA(Class*):
def __init__(self, lst, ...):
super(ClassA, self).__init__(...)
def meth1()
def meth2()
class ClassB(Class*):
def __init__(self, name, lst=None, other=None)
**here I want to use the __init__ method from ClassA**
self.a = ClassA(..)
def meth1():
return self.a.meth1()
I hope this is clear. Thanks!
In a composition, your ClassB simply has a member that happens to be a ClassA, so, you don't invoke ClassA's __init__ method directly, you just instantiate it if you need to.
class ClassB(Class*):
def __init__(self, name, lst=None, other=None):
self.my_A = ClassA(lst, ...)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
When you create 2 classes in Python, does the second class always have to be a subclass or the child class? Is it possible to have two classes that have object as their parameters? Thanks!
class Bird(object):
def __init__(self, name):
self.name = name
print("A %s has feathers" % self.name)
class Seagull(object):
def __init__(self):
print("Seagulls can fly")
super().__init__('Seagull')
seagull = Seagull()
What is wrong with this code? It says that Seagull is an inheritance so its (object) should be Bird... but why?
Whether you have subclasses or independent classes depends on the logic you're implementing.
With a bird and a seagull, you'd probably want a subclass, because a seagull is a kind of bird:
class Bird(object):
...
class Seagull(Bird):
...
In other situations, you would want separate classes, not related to each other:
class Bird(object):
...
class Locomotive(object):
...
By the way, in Python 3 the (object) part is not needed when it's literally object, so we would normally write:
class Bird:
...
class Seagull(Bird):
...
class Locomotive:
...
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
When I write the __init__ method and assign attributes, can I access those attributes in other methods (functions) that I write in that class? If so, how is it done?
I've googled this but couldn't find an answer. I Haven't been able to wrap my head around this one.
Use self:
class MyClass:
def __init__(self):
self.name = 'John'
def other_method(self):
print(self.name)
other_method will print "John".
When you make a class and set an instance (like first_class = MyClass()) the def __init__(self): is run or initialised. Any variables in there, like self.name are able to be accessed from within the class and its functions, as well as when you use a class in another program. self kinda attaches that variable to that class.
Basically using Allure's example:
class MyClass:
def __init__(self):
self.name = "John"
def show_name(self):
print(self.name)
Then use MyClass's name outside of class, in a program:
firstClass = MyClass()#Initialise MyClass and its variables
print(firstClass.name)
Or:
firstClass= MyClass()
firstClass.show_name()
Both output:
'John'
(still putting up this answer for others, hope you don't mind :) )
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I call ancestor init function but this doesn't call ancestor function set_param(), it calls descadent function.It means in my script, that fathers variable self.a isn't initialized. Can you explain it to me? Thank You.
class father (object):
def __init__(self):
self.set_param()
def set_param(self):
self.a = 'father' # isn't initialized
class son (father):
def __init__(self):
father.__init__(self)
self.set_param()
def set_param(self):
self.b = 'son'
person = son()
self.set_param() gets called twice, from son.__init__ and from father.__init__, but both times it calls son.set_param because type(self) is son in both cases. father.set_param is never called.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want a class that is just a complete door mat. If I call any method with any args, or any other attribute, it should just do nothing.
That is, given a class called Gobble:
gbl=Gobble()
gbl.foo.bar().asdf
should be fine.
A simple solution is
class Gobble(object):
def __getattr__(self, item):
return self
def __call__(self, *args, **kwargs):
return self
__getattr__ for property access, and __call__ for method call
You can use unittest.mock.MagicMock:
from unittest.mock import MagicMock
gbl=MagicMock()
gbl.foo.bar().asdf[1]
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
If I have some (and only one) class, A, in my file with some basic method,
class A(B):
def some_overrided_method()
return {'output': True}
what of the following would be better to use?
Static method inside class
class A(B):
def some_overrided_method(self)
return {'output': self.do_smth()}
#staticmethod
def do_smth(self):
return True
Function outside class
def do_smth():
return True
class A(B):
def some_overrided_method(self)
return {'output': do_smth()}
Of just some nested method inside
class A(B):
def some_overrided_method(self)
def do_smth():
return True
return {'output': do_smth()}
If it doesn't do anything with the class/instance, there is no point in it being a method of the class.
Just use a normal function.
A staticmethod is very rarely useful.
The only reason I can think of for using a staticmethod is if the function makes no sense to use outside of the class.