please help me on this topic : If I try to print object.dict, it only shows instance variables and not the methods where as classes do show it's variable and method functions.
If I consider powershell objects, then it does show it's method and properties and many more attributes.
example : powershell_object | get-member
you can use dir(obj) and get the methods and instance variables, then you can filter them to only functions like this:
object_functions = {}
for obj_member in dir(obj):
if callable(getattr(obj, obj_member)):
object_functions[obj_member] = getattr(obj, obj_member)
Methods are functions that belong to the objects's __class__, and to all the classes in it's class method-resolution order, which you can introspect using: __class__.__mro__
So consider:
In [15]: class A(object):
...: def amethod(self): pass
...: class B(A):
...: def bmethod(self): pass
...: class C(A):
...: def cmethod(self): pass
...: class D(B, C):
...: def dmethod(self): pass
...:
In [16]: d = D()
In [17]: vars(D)
Out[17]:
mappingproxy({'__module__': '__main__',
'dmethod': <function __main__.D.dmethod(self)>,
'__doc__': None})
Note, vars just returns __dict__. Notice, D.__dict__ only has dmethod. Now consider:
In [18]: D.__mro__
Out[18]: (__main__.D, __main__.B, __main__.C, __main__.A, object)
In [19]: from pprint import pprint
In [20]: for klass in D.__mro__:
...: print(klass, end=' '); pprint(vars(klass))
...:
<class '__main__.D'> mappingproxy({'__doc__': None,
'__module__': '__main__',
'dmethod': <function D.dmethod at 0x106e72048>})
<class '__main__.B'> mappingproxy({'__doc__': None,
'__module__': '__main__',
'bmethod': <function B.bmethod at 0x106e72ea0>})
<class '__main__.C'> mappingproxy({'__doc__': None,
'__module__': '__main__',
'cmethod': <function C.cmethod at 0x106e72158>})
<class '__main__.A'> mappingproxy({'__dict__': <attribute '__dict__' of 'A' objects>,
'__doc__': None,
'__module__': '__main__',
'__weakref__': <attribute '__weakref__' of 'A' objects>,
'amethod': <function A.amethod at 0x106e72400>})
<class 'object'> mappingproxy({'__class__': <attribute '__class__' of 'object' objects>,
'__delattr__': <slot wrapper '__delattr__' of 'object' objects>,
'__dir__': <method '__dir__' of 'object' objects>,
'__doc__': 'The most base type',
'__eq__': <slot wrapper '__eq__' of 'object' objects>,
'__format__': <method '__format__' of 'object' objects>,
'__ge__': <slot wrapper '__ge__' of 'object' objects>,
'__getattribute__': <slot wrapper '__getattribute__' of 'object' objects>,
'__gt__': <slot wrapper '__gt__' of 'object' objects>,
'__hash__': <slot wrapper '__hash__' of 'object' objects>,
'__init__': <slot wrapper '__init__' of 'object' objects>,
'__init_subclass__': <method '__init_subclass__' of 'object' objects>,
'__le__': <slot wrapper '__le__' of 'object' objects>,
'__lt__': <slot wrapper '__lt__' of 'object' objects>,
'__ne__': <slot wrapper '__ne__' of 'object' objects>,
'__new__': <built-in method __new__ of type object at 0x104ebe518>,
'__reduce__': <method '__reduce__' of 'object' objects>,
'__reduce_ex__': <method '__reduce_ex__' of 'object' objects>,
'__repr__': <slot wrapper '__repr__' of 'object' objects>,
'__setattr__': <slot wrapper '__setattr__' of 'object' objects>,
'__sizeof__': <method '__sizeof__' of 'object' objects>,
'__str__': <slot wrapper '__str__' of 'object' objects>,
'__subclasshook__': <method '__subclasshook__' of 'object' objects>})
Related
x = type.__dict__
x is x
gives,
True
but,
x = type.__dict__
operator = 'is'
eval(f'{x} {operator} {x}')
gives,
SyntaxError: invalid syntax
even,
operator = 'is'
eval(f'{x!r} {operator} {x!r}')
gives,
SyntaxError: invalid syntax
how to get True as output using eval and f-strings?
When using eval you are executing exactly what you wrote in the string. By formatting with the curly brakets you are inserting in the string the value of "x", or at least whatever is returned by the "__str__" method of x.
print(f"{x}")
gives
{'__repr__': <slot wrapper '__repr__' of 'type' objects>, '__call__': <slot wrapper '__call__' of 'type' objects>, '__getattribute__': <slot wrapper '__getattribute__' of 'type' objects>, '__setattr__': <slot wrapper '__setattr__' of 'type' objects>, '__delattr__': <slot wrapper '__delattr__' of 'type' objects>, '__init__': <slot wrapper '__init__' of 'type' objects>, '__new__': <built-in method __new__ of type object at 0x908780>, 'mro': <method 'mro' of 'type' objects>, '__subclasses__': <method '__subclasses__' of 'type' objects>, '__prepare__': <method '__prepare__' of 'type' objects>, '__instancecheck__': <method '__instancecheck__' of 'type' objects>, '__subclasscheck__': <method '__subclasscheck__' of 'type' objects>, '__dir__': <method '__dir__' of 'type' objects>, '__sizeof__': <method '__sizeof__' of 'type' objects>, '__basicsize__': <member '__basicsize__' of 'type' objects>, '__itemsize__': <member '__itemsize__' of 'type' objects>, '__flags__': <member '__flags__' of 'type' objects>, '__weakrefoffset__': <member '__weakrefoffset__' of 'type' objects>, '__base__': <member '__base__' of 'type' objects>, '__dictoffset__': <member '__dictoffset__' of 'type' objects>, '__mro__': <member '__mro__' of 'type' objects>, '__name__': <attribute '__name__' of 'type' objects>, '__qualname__': <attribute '__qualname__' of 'type' objects>, '__bases__': <attribute '__bases__' of 'type' objects>, '__module__': <attribute '__module__' of 'type' objects>, '__abstractmethods__': <attribute '__abstractmethods__' of 'type' objects>, '__dict__': <attribute '__dict__' of 'type' objects>, '__doc__': <attribute '__doc__' of 'type' objects>, '__text_signature__': <attribute '__text_signature__' of 'type' objects>}
Meaning that's what you are trying to execute, which is indeed invalid syntax.
You should instead do:
eval("x is x")
Works for me:
>>> d = {"a":42}
{'a': 42}
>>> print(f"{d} is {d}")
{'a': 42} is {'a': 42}
>>> {'a': 42} is {'a': 42}
False
>>> eval(f"{d} is {d}")
False
This works just fine with dicts.
However, calling str.__dict__ returns something completely different than you'd probably expect:
>>> str.__dict__
mappingproxy({
'__new__': <built-in method __new__ of type object at 0x00007FFC55D1AC60>,
'__repr__': <slot wrapper '__repr__' of 'str' objects>,
--- snip for readability ---
This "dict-representation" is actually a MappingProxy as you can verify with type(str.__dict__) and cannot exactly be re-interpreted by eval() as <built-in method __new__... is obviously a syntax error, when used without quotes. You can easily verify this by pasting this into a Python interpreter:
>>> {"myKey": <built-in method __new__ of type object at 0x00007FFC55D1AC60>}
File "<stdin>", line 1
{"myKey": <built-in method __new__ of type object at 0x00007FFC55D1AC60>}
^
SyntaxError: invalid syntax
What you're trying to do requires not only type (whatever this might be at runtime) to have a dict-representation, but also every member to have one - recursively! This is hard to assume in a general case.
I'm convinced what you're trying to achieve can be done using a different way - if you want elaborate your task at hand, there might be a more suitable way.
If you insist on the eval() way, take a look at repr() and eval(), as generally, repr tries to produce a string, which can be fed into eval to produce the very same object again.
However, this is possible only, if the object implements repr to do exactly that - which is not guaranteed. There might be objects which cannot be serialized that way.
myDict = {"myKey": "myVal"}
myDict == eval(repr(myDict))
# Output: True
To get the same as x is x through eval you need the name of the variable as a string "x" instead of the variable itself. How to get it is answered here: Getting the name of a variable as a string
Here is a solution for your case:
x = type.__dict__
operator = 'is'
x_name = f'{x=}'.split('=')[0]
eval(f'{x_name} {operator} {x_name}')
Resulting in:
> True
I have a python internal class that looks like this:
class TopClass:
#my_decorator
class InternalClass:
"""Persistent state."""
a: int
b: int
c: int
in this case my_decorator is a thin wrapper around #dataclass. Running sphinx however writes in the docs things like:
__dataclass_params__= _DataclassParams(init=True,repr=True,eq=True,order=False,unsafe_hash=False, frozen=False)
__dict__= mappingproxy({'__module__': 'my_module.try', '__annotations__': {'a': 'int', 'b': 'int', 'c': 'int'}, '__doc__': 'Persistent state.', '__dict__': <attribute '__dict__' of 'InternalClass' objects>, '__weakref__': <attribute '__weakref__' of 'State' objects>, '__dataclass_params__': _DataclassParams(init=True,repr=True,eq=True,order=False,unsafe_hash=False,frozen=False), '__dataclass_fields__': {'a': Field(name='a',type='int',default=<dataclasses._MISSING_TYPE object>,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),_field_type=_FIELD), 'b': Field(name='b',type='int',default=<dataclasses._MISSING_TYPE object>,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),_field_type=_FIELD), 'c': Field(name='c',type='int',default=<dataclasses._MISSING_TYPE object>,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),_field_type=_FIELD)}, '__init__': <function __create_fn__.<locals>.__init__>, '__repr__': <function __create_fn__.<locals>.__repr__>, '__eq__': <function __create_fn__.<locals>.__eq__>, '__hash__': None})
__eq__(other)
Return self==value.
__hash__= None
__init__(a, b, c)
How can I avoid this?
I am trying to design an API in python using a class:
class SimulationApi(object):
def hello(self):
return "Hi"
def echo(self, string):
return string
def get_foo(self):
return self.foo
def __init__(self):
self.foo = 50
And i'd like to print to console a list of available public methods defined by the class. Is there a way to to this automatically, that will also pick up the method parameters? Ideally, the output would look like this:
SimulationApi:
get_foo()
echo(string)
hello()
So far, my solution is this, but it is not complete and may be the wrong direction.
print("SimulationApi: \n\t{}\n".format("\n\t".join([x+"()" for x in dir(SimulationApi) if not x.startswith("__")]))
You can use inspect module:
class SimulationApi(object):
def hello(self):
return "Hi"
def echo(self, string):
return string
def get_foo(self):
return self.foo
def __init__(self):
self.foo = 50
import inspect
inspect.getmembers(SimulationApi)
Will return:
[('__class__', type),
('__delattr__', <slot wrapper '__delattr__' of 'object' objects>),
('__dict__',
mappingproxy({'__module__': '__main__',
'hello': <function __main__.SimulationApi.hello(self)>,
'echo': <function __main__.SimulationApi.echo(self, string)>,
'get_foo': <function __main__.SimulationApi.get_foo(self)>,
'__init__': <function __main__.SimulationApi.__init__(self)>,
'__dict__': <attribute '__dict__' of 'SimulationApi' objects>,
'__weakref__': <attribute '__weakref__' of 'SimulationApi' objects>,
'__doc__': None})),
('__dir__', <method '__dir__' of 'object' objects>),
('__doc__', None),
('__eq__', <slot wrapper '__eq__' of 'object' objects>),
('__format__', <method '__format__' of 'object' objects>),
('__ge__', <slot wrapper '__ge__' of 'object' objects>),
('__getattribute__', <slot wrapper '__getattribute__' of 'object' objects>),
('__gt__', <slot wrapper '__gt__' of 'object' objects>),
('__hash__', <slot wrapper '__hash__' of 'object' objects>),
('__init__', <function __main__.SimulationApi.__init__(self)>),
('__init_subclass__', <function SimulationApi.__init_subclass__>),
('__le__', <slot wrapper '__le__' of 'object' objects>),
('__lt__', <slot wrapper '__lt__' of 'object' objects>),
('__module__', '__main__'),
('__ne__', <slot wrapper '__ne__' of 'object' objects>),
('__new__', <function object.__new__(*args, **kwargs)>),
('__reduce__', <method '__reduce__' of 'object' objects>),
('__reduce_ex__', <method '__reduce_ex__' of 'object' objects>),
('__repr__', <slot wrapper '__repr__' of 'object' objects>),
('__setattr__', <slot wrapper '__setattr__' of 'object' objects>),
('__sizeof__', <method '__sizeof__' of 'object' objects>),
('__str__', <slot wrapper '__str__' of 'object' objects>),
('__subclasshook__', <function SimulationApi.__subclasshook__>),
('__weakref__', <attribute '__weakref__' of 'SimulationApi' objects>),
('echo', <function __main__.SimulationApi.echo(self, string)>),
('get_foo', <function __main__.SimulationApi.get_foo(self)>),
('hello', <function __main__.SimulationApi.hello(self)>)]
Note: Your methods (the methods you like to get info) are also the SimulationApi class dictionary __dict__.
You can get the full code for your echo function like this:
import inspect
lines = inspect.getsource(SimulationApi.echo)
print(lines)
def echo(self, string):
return string
I discovered a weird behaviour (at least weird for me) on python class variables.
class Base(object):
_var = 0
#classmethod
def inc_class(cls):
cls._var += 1
#staticmethod
def inc_static():
Base._var += 1
class A(Base):
pass
class B(Base):
pass
a = A()
b = B()
a.inc_class()
b.inc_class()
a.inc_static()
b.inc_static()
print(a._var)
print(b._var)
print(Base._var)
The output is 1 1 2.
This is surprising me (I was expecting 4 4 4) and I'm wondering why?
When decorated with #classmethod the first argument cls to inc_class(cls) is, well, the class. <class '__main__.A'> and <class '__main__.B'> respectively for A and B. So cls._var refers to A's _var, and similarly for B. In inc_static, decorated with #staticmethod there is no argument, you're explicitly referring to <class '__main__.Base'>, a different _var.
Note the '_var': 0 attribute in Base's and A's __dict__. #classmethod is doing what you'd expect it to do, binding members to classes, in this case A and B.
>>> Base.__dict__
mappingproxy({'__module__': '__main__', '_var': 0, 'inc_class': <classmethod
object at 0x7f23037a8b38>, 'inc_static': <staticmethod object at
0x7f23037a8c18>, '__dict__': <attribute '__dict__' of 'Base' objects>,
'__weakref__': <attribute '__weakref__' of 'Base' objects>, '__doc__': None})
>>> A.__dict__
mappingproxy({'__module__': '__main__', '__doc__': None})`
After calling Base.inc_static():
>>> Base.__dict__
mappingproxy({'__module__': '__main__', '_var': 1, 'inc_class':
<classmethod object at 0x7f23037a8b38>, 'inc_static': <staticmethod
object at 0x7f23037a8c18>, '__dict__': <attribute '__dict__' of 'Base'
objects>, '__weakref__': <attribute '__weakref__' of 'Base' objects>,
'__doc__': None})
>>> A.__dict__
mappingproxy({'__module__': '__main__', '__doc__': None})
After calling A.inc_class():
>>> Base.__dict__
mappingproxy({'__module__': '__main__', '_var': 1, 'inc_class':
<classmethod object at 0x7f23037a8b38>, 'inc_static': <staticmethod
object at 0x7f23037a8c18>, '__dict__': <attribute '__dict__' of 'Base'
objects>, '__weakref__': <attribute '__weakref__' of 'Base' objects>,
'__doc__': None})
>>> A.__dict__
mappingproxy({'__module__': '__main__', '__doc__': None, '_var': 1})
What's interesting is how A's _var is initialised. Note that you do cls._var += 1 before cls._var has been defined. As explained here, cls._var += 1 is equivalent to cls._var = cls._var; cls._var += 1. Because of the way python does lookup the first read of cls._var will fail in A and continue to find it in Base. At the assignment _var is added to A's __dict__ with the value of Base._var, and then all is fine.
>>> class Base(object):
... _var = 10
... #classmethod
... def inc_class(cls):
... cls._var += 1
...
>>> class A(Base):
... pass
...
>>> A.__dict__
mappingproxy({'__module__': '__main__', '__doc__': None})
>>> A.inc_class()
>>> A.__dict__
mappingproxy({'__module__': '__main__', '__doc__': None, '_var': 11})
Even though the two classes inherit from the Base class, they are completely different objects. Through the instantiation of a and b, you have two objects that belong to two separate classes. When you call
a.inc_class()
b.inc_class()
you increment the _var attribute of class A once, and then you do the same for class B. Even though they share the same name, they are different objects. If you had a second instance of class A, say a2, and you would call the function again, then both calls would manipulate the same variable. This explains how you get your first two outputs.
The third output refers to the Base class object. Again, even though it is the same name, it is a different object. You increment the 3rd object twice, therefore you get 2 as the answer.
I am looking at a code given to me by a co-worker who no longer works with us.
I have a list variable called rx.
>> type(rx)
type 'list'
When I go to look inside rx[0] I get this:
>> rx[0]
<Thing.thing.stuff.Rx object at 0x10e1e1c10>
Can anyone translate what this means? And, more importantly, how can I see what is inside this object within the rx list?
Any help is appreciated.
Start with help: help(rx[0])
# example python object
class Employee:
"""Common base class for all employees."""
empCount = 0
help(Employee)
Output:
Help on class Employee in module __main__:
class Employee(builtins.object)
| Common base class for all employees.
|
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| empCount = 0
If that's not enough info check out the inspect module.
Inspect has a lot of methods that might be useful, like getmembers and getdoc:
import inspect
inspect.getdoc(Employee) # 'Common base class for all employees.'
for name, data in inspect.getmembers(Employee):
if name == '__builtins__':
continue
print('%s :' % name, repr(data))
Output:
__class__ : <class 'type'>
__delattr__ : <slot wrapper '__delattr__' of 'object' objects>
__dict__ : mappingproxy({'__module__': '__main__', '__dict__': <attribute '__dict__' of 'Employee' objects>, '__weakref__': <attribute '__weakref__' of 'Employee' objects>, 'empCount': 0, '__doc__': 'Common base class for all employees.'})
__dir__ : <method '__dir__' of 'object' objects>
__doc__ : 'Common base class for all employees.'
__eq__ : <slot wrapper '__eq__' of 'object' objects>
__format__ : <method '__format__' of 'object' objects>
__ge__ : <slot wrapper '__ge__' of 'object' objects>
__getattribute__ : <slot wrapper '__getattribute__' of 'object' objects>
__gt__ : <slot wrapper '__gt__' of 'object' objects>
__hash__ : <slot wrapper '__hash__' of 'object' objects>
__init__ : <slot wrapper '__init__' of 'object' objects>
__le__ : <slot wrapper '__le__' of 'object' objects>
__lt__ : <slot wrapper '__lt__' of 'object' objects>
__module__ : '__main__'
__ne__ : <slot wrapper '__ne__' of 'object' objects>
__new__ : <built-in method __new__ of type object at 0x108a69d20>
__reduce__ : <method '__reduce__' of 'object' objects>
__reduce_ex__ : <method '__reduce_ex__' of 'object' objects>
__repr__ : <slot wrapper '__repr__' of 'object' objects>
__setattr__ : <slot wrapper '__setattr__' of 'object' objects>
__sizeof__ : <method '__sizeof__' of 'object' objects>
__str__ : <slot wrapper '__str__' of 'object' objects>
__subclasshook__ : <built-in method __subclasshook__ of type object at 0x7faa994086e8>
__weakref__ : <attribute '__weakref__' of 'Employee' objects>
empCount : 0