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]
Related
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 days ago.
Improve this question
We have a class A, and we want to define a wrapper class B that just changes A's defaults first arg.
What's the pythonic way to do this?
option 1)
Using super class
class B(A):
def __init__(self, *args, **kwargs):
a = 1
super().__init__(a=a, *args, **kwargs)
option 2)
Using function
def get_A(*args,**kwargs):
a = 1
return A(a=a, *args, **kwargs)
My question is general for python's code style, 'cause i can't find the answer in pep8.
But If it helps, in my usecase arg a is a long nn.Sequential, that i want to use several times (with different weights)
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 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 4 years ago.
Improve this question
I'm not able to set value to class variable in parent class but able to print it.It is saying attribute doesn't exist even when i am able to print it.
CODE:
class Base(object):
x=20
class Derived(Base):
def __init__(self):
print(super().x)
super().x=80
obj=Derived()
A Derived is a Base, i.e. all instances of Derived can be treated exactly like instances of Base. In your case, that means you simply set self.x = 80.
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, ...)
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 8 years ago.
Improve this question
I'm refactoring a program of mine. Basically I move all classes into a module.
Now I'm facing the problem that some of the module code is dependent on instances of a class I instantiated in my main program. Of course I could pass the instance to the method directly. Or pickle the instance. Or define the attribute as global. Which is the best way to go?
One possibility might be to pass the instance to the class upon instantiation:
class Bar(object):
def __init__(self, inst):
self.inst = inst
def method(self):
# use self.inst
inst = Foo()
bar = Bar(inst)