How to access instance variable inside #classmethod? - python

I have two class as below:
class A:
def __init__(self, a):
self.a = a
class B(A):
#classmethod
def good(cls):
return cls.a
a = B('good')
When I run print(a.good())
It shows AttributeError: type object 'B' has no attribute 'a'
How to access a variable from good method?

In no way.
#classmethods do not have access to instances of the class.

Related

python instantiate child instance from parent object

I have an object which is instantiated from parent class(it will b variable at below example code)
and i want to use this object like a child class instance without knowledge about the member variable of parent class
is there any recommendation?
class A:
def __init__(self):
pass # some member variables are assigned
pass
class B(A):
def test(self):
print("test")
pass
b = A()
b.test() # error
You can do this by setting __class__ of b to B. But read this first:
https://stackoverflow.com/a/13280789/6759844
class A:
def __init__(self):
pass # some member variables are assigned
pass
class B(A):
def test(self):
print("test")
pass
b = A()
b.__class__ = B
b.test() # error

Identify the superclass that defines a class-level variable

In the case of multiple inheritance in python, is there a way to identify which super class a class-level variable is obtained from?
All attempts I tried to google are overwhelmingly about How to get the attribute not find out where it came from:
https://www.google.com/search?q=pythin+which+super+class+defines+attr
https://www.google.com/search?q=python+which+super+class+has+attribute&oq=python+which+super+class+has+attr
https://www.google.com/search?q=python+which+super+class+attribute+obtained+from
I suppose I can manually step through the MRO using inspect.getmro(cls). But I couldn't find any more elegant solutions. Just wondering if anyone knows of one.
EDIT
For a concrete example:
class Super1(object):
__class_attribute__ = "Foo"
class Super2(object):
pass
class Derived(Super1, Super2):
pass
d = Derived()
parent_cls = some_function_to_get_defining_class(d.__class_attribute__) # <-- should return `Super1`
The __qualname__ attribute gives an indication from which class a method was inherited. However, this only returns a string, not the superclass itself. If you need to the superclass for metaprogramming, I think you are going to have to dig into the MRO.
class A:
def a(self):
return 1
def b(self):
return 2
class B:
def b(self):
return 2.5
def c(self):
return 3
class C(A,B):
pass
Using:
C.b.__qualname__
# returns:
'A.b'
However, this does not apply when using abstract methods to define an interface, since the method has to be overwritten.
from abc import abstractmethod
class A:
def a(self):
return 1
#abstractmethod
def b(self):
pass
class C(A):
def b(self):
return 100
C.b.__qualname__
# returns:
'C.b'

Access class which object is being called from within method belonging to object

I wonder if there is a way in Python to access the class which the object which the method belongs to is being called from. For example:
class A:
def __init__(self):
self.b = B()
def foo(self):
print('A')
class B:
def bar(self):
<something here>.foo()
a = A()
a.b.bar()
Basically I would like B's method bar to invoke A's method foo. And if b was an attribute of some other class C, to invoke C's version of foo instead.
You could add a reference to the class which instantiates B:
class A:
def __init__(self):
# pass self while instantiating B
self.b = B(self)
def foo(self):
print('A')
class B:
def __init__(self, rel_obj):
self.rel_obj = rel_obj
def bar(self):
self.rel_obj.foo() # access foo() using self.rel_obj
Similarly, you could pass an object of class C to invoke C's version of foo method.

Python: Access base class "Class variable" in derive class

I am trying to access a class variable from the base class in the derived class and I am getting a no AttributeError
class Parent(object):
variable = 'foo'
class Child(Parent):
def do_something(self):
local = self.variable
I tried using it as Parent.variable but that did not work either. I am getting the same error
AttributeError: 'Child' object has no attribute 'Child variable'
How do i resolve this
I'm not sure what you're doing wrong. The code below assumes you have an initialization method, however.
class Parent(object):
variable = 'foo'
class Child(Parent):
def __init__(self):
pass
def do_something(self):
local = self.variable
print(local)
c = Child()
c.do_something()
Output:
foo
The code shown below should work on both Python 2 & 3:
class Parent(object):
variable = 'foo'
class Child(Parent):
def do_something(self):
local = self.variable
c = Child()
print(c.variable) # output "foo"

How to access an instance variable of a parent class in python?

I want to access a class variable defined in the parent class constructor. Here is the code.
class A(object):
def __init__(self):
x = 0
class B(A):
def __init__(self):
super(B, self).__init__()
def func(self):
print self.x
s = B()
s.func()
This gives me error:
AttributeError: 'B' object has no attribute 'x'
If I try changing the func() to
def func(self):
print x
then I get error:
NameError: global name 'x' is not defined
If I try changing the func() to
def func(self):
print A.x
Then I get the error
AttributeError: type object 'A' has no attribute 'x'
Now I am running out of ideas.. What's the correct way to access that class variable x in the parent class A? Thanks!
NOTE: I am working only on the "class B" part of my project, hence I can't really go modify class A and change the way variables are defined. That's the only constraint.
It must be self.x, not just x:
class A(object):
def __init__(self):
self.x = 0
Just a quick note - even from other methods of A would be the x not accessible:
class A(object):
def __init__(self):
x = 0
def foo(self):
print(self.x) # <- will not work
print(x) # <- will utimately not work
You need to set it as self.x = 0 instead of x = 0 - otherwise it's just a local variable.
If you cannot modify A, what you are trying to do is impossible - there is absolutely no way to access that value, not even with black magic (if your method was called by that method, you could probably do nasty things with the stack to get its value)

Categories