How to get class name from bound method using inspect? [duplicate] - python

This question already has answers here:
How to get the caller class name inside a function of another class in python?
(6 answers)
Closed 7 years ago.
class MyClass:
def my_method(self):
print(get_context())
MyClass().my_method()
I need get next line:
MyClass::my_method
sys._getframe(2).f_code.co_name gives me only "my_method". How to get also class name?

You can get your classname by calling __class__.__name__ from self.
class Foo(object):
def bar(self):
print(self.__class__.__name__)
Foo().bar()
Output:
Foo

Related

How to add new methods to a class after importing it? [duplicate]

This question already has answers here:
How to extend a class in python?
(4 answers)
Closed 12 months ago.
How can we add methods to a class that we imported from another code file in python?
You can extend the class like this:
class a:
def __init__(self):
self.value = "42"
class b(a):
def printValue(self):
print(self.value)
b().printValue()
from other_library import ThatClass
class NewClass(ThatClass):
def new_method(self):
# Implement here
pass

Set instance variable name based on argument passed in Python class [duplicate]

This question already has answers here:
Why use setattr() and getattr() built-ins?
(5 answers)
Closed 1 year ago.
I want to be able to set the instance variable name in a Python class based on an argument in __init__. Is this possible? Below is what I've tried but that doesn't work.
class my_class:
def __init__(self, var_name):
self['var' + var_name] = var_name
Something like the below.
(Question: why do you want to do that?)
class MyClass:
def __init__(self, var_name):
setattr(self,var_name,var_name)
c = MyClass('jack')
print(c.jack)
output
jack

How to access a class attribute using a string? [duplicate]

This question already has answers here:
obtaining named attributes of self
(2 answers)
Closed 3 years ago.
I want to know how can I access a class attribute using a string. E.g
class Test:
def __init__(self):
# defined self.name here
self.name.person = 1
If I have the program:
a = "person"
b = Test()
How can I print self.name.person using the variable a?
print(b.name.a)
Many thanks!
Python has getattr() as built-in function:
print(getattr(b.name, a))

Get name of module that is instantiating my class [duplicate]

This question already has answers here:
How to get the caller class name inside a function of another class in python?
(6 answers)
Closed 4 years ago.
I am editing some code that is being called by another system that I do not control. This system initializes my class from a few different places. I need to perform different things depending on where my class is called from.
Is there a way I can find where my object is being initialized?
A.py:
class InitializerA:
def calling_function(self):
Called()
class InitializerB:
def calling_function(self):
Called()
B.py:
class Called:
def __init__(self):
# I want to know here whether it is being called by InitializerA or InitializerB
I was able to find the answer by modifying the method given in this question:
How to get the caller class name inside a function of another class in python?
The inspect library helped me inspect the call stack.
You could pass the class initializing it as an parameter to Called i.e:
class class InitializerA:
def calling_function(self):
Called(self.__name__)
class Called:
def __init__(self, initializer):
self.initializer = initializer

python #property not overwrite class attribute [duplicate]

This question already has answers here:
Python method name with double-underscore is overridden?
(2 answers)
Closed 6 years ago.
I'm confused with the class attribute. I understand that Python interpreter will search attr inside cls.__dict_ (object attribute) first, If the attribute doesn't exists, it will looking for at class attributes. But in that case I dont know why the result below return None
class A(object):
__attr = None
#property
def attr(self):
return self.__attr
class B(A):
__attr = 1
c = B()
print(c.attr)
# None
This isn't about property, but about the behaviour of attributes prefixed with __. This triggers name mangling, which is almost never what you want and behaves unexpectedly in an inheritance scenario. Don't use it.

Categories

Resources