Python: how to maintain independence from instance? [closed] - python

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)

Related

How to call a function defined in a class, outside of its class it is defined in? [closed]

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
I made a function drive_car inside of the class car, but I do not know how to call it from the class it is defined in, outside of the class, while staying inside of its class.
class car:
def drive_car():
print("car go vroom vroom")
car.drive_car() # -- gives error message
There are 2 total ways you can call drive_car()
Convert drive_car() into a static method. This way you can call this via car.drive_car()
Or, you instantiate the class car and then call drive_car() like so: car().drive_car()

class A :, class A(object) :, or class A() : [closed]

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 2 years ago.
Improve this question
What is the exact difference among these three and when do we use what?
class A:
pass
class A(object):
pass
or
class A():
pass
class A:
pass
It is implicitly subclass of object (as in other cases). I think it's most prefereable in case you don't inherit from anything (but it can depend on coding standards).
2)
class A(object):
pass
It is most explicit version.
3)
class A():
pass
In this case, as no class passed as parent class, by default it inherits from object.
So from functionality point of view, there is no difference. In Python3, all clasess inherit from object (even if it's not explixitly declared).
However, if you are using Python2, you need to pass superclass explicitly in every case.

Calling ancestor function [closed]

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.

Make a class that does nothing [closed]

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]

not able to set value using super().x [closed]

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.

Categories