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))
Related
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
This question already has answers here:
How do I get the string with name of a class?
(5 answers)
Closed 4 years ago.
class A:
pass
> A().__class__.__name__
-->'A'
> A.__class__.__name__
--> 'type'
How can I get 'A' without the parens?
You're getting 'type', because the class of a class definition is... type (in other words: a class definition is a type).
You can just use the __name__ attribute. No need to look for the __class__, you already have the class:
A.__name__
'A'
Just to make it completely clear:
A().__class__ is A
True
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
This question already has answers here:
Can you monkey patch methods on core types in Python?
(15 answers)
Closed 8 years ago.
"this is my string".myfunction(argument)
This is very simple in javascript. With the keyword this i can access to my string directly. Is that possible with python?
You can inherit from str and define your own methods:
class myString(str):
def my_method(self, ...):
# ...
some_string = myString("StackOverflow")
print some_string.count("a") # method from string
print some_string.myMethod(...) # your defined method
This question already has answers here:
List attributes of an object [duplicate]
(18 answers)
Closed 4 years ago.
For example I have next python class
class Myclass():
a = int
b = int
Imagine that I don't know the name this class, so I need to get the names of attributes? ("a" and "b")
If you want all (including private) attributes, just
dir(Myclass)
Attributes starting with _ are private/internal, though. For example, even your simple Myclass will have a __module__ and an empty __doc__ attribute. To filter these out, use
filter(lambda aname: not aname.startswith('_'), dir(Myclass))