Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
__dict__ in math library
#1
hi
i saw that there is no "__dict__" in math library. namely:
import math
"__dict__ " in dir(math)
the result will be False. but when i write
math.__dict__.items() 
, a large dict_items object will be shown in idle.
how is it possible when there is not __dict__ in math lib,then there is math.__dict__.items() ?
thanks
Reply
#2
https://docs.python.org/3/library/stdtyp...attributes

Quote:Special Attributes

The implementation adds a few special read-only attributes to several object types, where they are relevant. Some of these are not reported by the dir() built-in function.

object.__dict__


A dictionary or other mapping object used to store an object’s (writable) attributes.
akbarza and Gribouillis like this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
hi
as i said before, the
math.__dict__.items()
gives a dict_items object with first element( as i run) :
('__names__','math') .
how can I get this element? how can I get '__names__' ? and how for 'math'?
thanks
Reply
#4
Get attribute values the same way as any other object.
import math

print(math.__name__)
print(getattr(math, "__name__"))
You should not be using __dict__ directly. Not all objects have __dict__. Some have __slots__, but dir() works for both.
class Base:
    __slots__ = "foo", "bar"


class Right(Base):
    __slots__ = ("baz",)


r = Right()
print(dir(r))
print(r.__dict__)
Error:
Traceback (most recent call last): File "...", line 11, in <module> print(r.__dict__) AttributeError: 'Right' object has no attribute '__dict__'. Did you mean: '__dir__'?
Reply
#5
hi deanhystad
is there a way to print all elements in math.__dict__.names()? namely as:
('__names__','math')
( .......)
(...)
.
.
.

how?
Reply
#6
hi
excuse me for more questions
math.__dict__.names() is of type dict_items type. what is the difference between 'dict_items' type and the 'dict' type?
in page https://realpython.com/python-eval-funct...with-input there is a code( large code, so i not import here) that contains the below line:
ALLOWED_NAMES = {
    k: v for k, v in math.__dict__.items() if not k.startswith("__")
}
so if I write:
for k, v in math.__dict__.items():
      print(k,v)
it prints something.
but for a dictionary as :
dic={'x': 1, 'y': 2, 'z': 3, 'w': 4}
then :
for a,b in dic:
    print(a,b)
then idle creates an error:
Error:
Traceback (most recent call last): File "<pyshell#39>", line 1, in <module> for a,b in dic: ValueError: not enough values to unpack (expected 2, got 1)
what is the difference between them? thanks for more explain.
Reply
#7
when you iterate over dict, you iterate over keys only, i.e. for key in some_dict: is equivalent to for key in some_dict.keys():. You need to iterate over some_dict.items() if you want to get both key and value, i.e. you get (key, value) tuple which you unpack, in your case into a and b respectively
akbarza likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  math.log versus math.log10 stevendaprano 10 2,427 May-23-2022, 08:59 PM
Last Post: jefsummers
  Why getting ValueError : Math domain error in trig. function, math.asin() ? jahuja73 3 3,783 Feb-24-2021, 05:09 PM
Last Post: bowlofred
  dict and __dict__ cls0724 1 2,693 Apr-05-2020, 07:45 PM
Last Post: Mateusz
  what is the __dict__ object? aaron1989041 6 4,773 Mar-29-2018, 05:18 AM
Last Post: Gribouillis
  PyInstaller, how to create library folder instead of library.zip file ? harun2525 2 4,816 May-06-2017, 11:29 AM
Last Post: harun2525

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020