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
Related
This question already has answers here:
What is the difference between a function, an unbound method and a bound method?
(6 answers)
Get an object attribute [duplicate]
(4 answers)
Closed 3 years ago.
I want to call class member function through object .The function is mapped in dictionary to a string. I will actually refer the string(key) to call the function(value). It is giving me error that 1 positional argument is required even when I pass the argument. Why do I have to pass the obj name?
When I pass the object instance as 1st parameter, it works.
class A():
def foo(self , arg):
print(arg)
dict = {"foo" :foo}
outside class :
obj = A()
obj.dict["foo"](10) #not working
#------------------------
obj.dict["foo"](obj,10) #working
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))
This question already has answers here:
Can I dynamically convert an instance of one class to another?
(5 answers)
Assigning to an instance's __class__ attribute in Python
(1 answer)
Closed 4 years ago.
Is it possible to change the class of an object? In the following example:
class Fisch:
...
class Trout(Fish):
def some_special_trout_method:
...
...
class Salmon(Fish):
...
...
fisch_object = Trout()
Would it be possible to change the trout into a salmon and keep the class variable values?
If this is possible how? And if it's possible, is it a good idea?
This question already has answers here:
How does the #property decorator work in Python?
(15 answers)
How do Python properties work?
(4 answers)
Closed 4 years ago.
I can do
class Foo(object):
x = property(lambda _: 123)
f = Foo()
f.x
to get 123
However, if I try
p = property(lambda : 123)
p
I get
<property object at 0x108f2f3b8>
Now I understand that an member of a class instance is not the same thing as a regular variable but I'm not sure what exactly makes this behavior different.
Does the fact that you instantiate a class somehow do extra binding on property objects? Is it a special case or is it a behavior I can take advantage in other situations and extend? Related - are property objects useful outside of a class declaration? Or is it just for this specific case?
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))