reference to creating class instance for class defined within creating class - python

I have the following structure:
class foo(object):
class bar(object):
def __init__(self, parent):
self._parent=parent #this
def worker(self):
return self._parent.name
def __init__(self, name):
self.name=name
def giveNamePointer(self):
return self.bar(self) #and this
Which works fine, however I was wondering if there is an implicit or easier way to get the reference to the creating instance in the special case, that the created instance is a class defined in the creating class.
edit: could this help me :implementing descriptiors and if so how?

No. Explicit is better than implicit.
(There's nothing special about defining a class inside another class.)

Related

Creating class object inside same class in Python

I'm trying to create a class object inside the class itself, something like this:
class Motor:
#other code here
def function(self):
x = Motor()
This works and runs correctly, but if I use inheritance to create another class as a daughter of this one (e.g. Car(Motor)), the Car.function() doesn't work properly because it's creating an object of type Motor, not Car. Is there any other way I could do this?
Thanks!!
You'll want to receive the class to instantiate as an argument to the method, rather than hard-coding the class name.
class Motor:
#classmethod
def function(cls):
x = cls()
...
If this is really something that needs to be an instance method, you can use type(self) instead.
class Motor:
def function(self):
x = type(self)()

Accessing a nested class via "self"?

So, I have a situation as described as follow:
class A(object):
def __init__(self):
pass
def foo(self):
a = self.InnerClass()
a.hooo()
class InnerClass(object):
def hooo(self):
print 'Yeah!'
class DummyA(A):
class InnerClass(A.InnerClass):
def hooo(self):
print 'Yay!'
class Test(object):
x = DummyA()
x.foo()
A is my main class which I want to test. Then there is an inner class whose function hooo() is getting called. I want to override that function in my DummyA class. so, I did that as described in code. It works and the output is 'Yay!'. I have seen in many examples online that outer class always accesses inner class by this syntax. OuterClass.InnerClass but in my case I have used self to access inner class. Is it bad practice? if so, for what reason? Any other way I can test the function hooo?

inheriting python method with a call to super inside the method

I am developing a system, which has a series of single multilevel inheritance hierarachy. one of the methods (applicable to all the classes) has to perform the same thing for most of the classes, which is to pass a list to its parent class.
I know that if one doesn't define a method in one of the inherited classes, its parents' methods are used. But when we use the super method, we need to mention the name of the class being called.
One method I know to achieve this is to redefine the method at every class with class name as argument. Is there any elegant method where I can define it once at the topmost parent, and then override it only when necessary?
The implementation right now looks like this
class a(object):
def __init__(self):
self.myL = list()
print 'hello'
class b(a):
def __init__(self):
super(b,self).__init__()
def resolve(self, passVal):
print passVal
self.myL.append(passVal)
super(b,self).resolve(passVal+1)
class c(b):
def __init__(self):
super(c,self).__init__()
def resolve(self, passVal):
print passVal
self.myL.append(passVal)
super(c,self).resolve(passVal+1)
Instead if I can define resolve in class a, and then all other classes inherit the method from it. I understand a will never be able to use it. but redefining the method seems a lot unnecessary extra work.

Creating a Class Object from the String Name, Python

I have a python module that calls multiple other classes within the same module.
class Main(object):
def foo(self):
return 'bar'
def getClass(self, className, *args):
return eval(className + "(%s)" % ",".join(args))
class A(object):
def __init__(self, b):
self._b = b
class B(object):
def __init__(self, c):
self._c = c
What I would like to do is this. In the class Main, I want to generate a class Object by only knowing the class name, and passing in the variables needed to create that class.
I now I can do conditional if/else, but I was wondering if it was possible. I figured I could use eval() as well, but i heard that can be evil.
Suggestions? Comments? I have a class that references multiple sublcasses, and instead of creating a class for each type, I figured this would be easier to do.
Thank you

When accessing Properties, send to another instance

class x():
def __init__(self):
self.z=2
class hi():
def __init__(self):
self.child=x()
f=hi()
print f.z
I want it to print 2.
Basically I want to forward any calls to that class to another class.
The simplest approach is implementing __getattr__:
class hi():
def __init__(self):
self.child=x()
def __getattr__(self, attr):
return getattr(self.child, attr)
This has certain disadvantages, but it might work for your limited use case. You might want to implement __hasattr__ and __setattr__ as well.
The Python syntax is:
class hi(x):
To say that hi inherit (should be a child of) x.
.
Note: in order for hi to have property z (since this is in hi's __init__) x.__init__ needs to be explicitly run in x. That is,
class hi(x):
def __init__(self):
x.__init__(self)

Categories