0

The title is content. To get down to business, you need to look at the contents below.

class Test():
    def __init__(self, name, power):
        print(f'class dir : {dir(self.__class__)}', end='\n\n')
        print(f'class name : {self.__class__.__name__}')
        self.__name = name + self.__class__.__name__
        self.__power = power
    def use(self):
        print(f'blah~')
    @property
    def name(self):
        return self.__name

>>>Test('test', 1)
class dir : ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name', 'use']
class name : Test

How can '__name__' object be used when there is no '__name__' object in dir(self.__class__)?

I really don't know why this is happend. Please, share you experience. thank you.

  • From the documentation: "Because `dir()` is supplied primarily as a convenience for use at an interactive prompt, it tries to supply an interesting set of names more than it tries to supply a rigorously or consistently defined set of names, and its detailed behavior may change across releases." – jasonharper May 27 '21 at 03:38
  • @jasonharper thanks for you comment. But, I wanna know that exactly ;_; – gangmin.chun May 27 '21 at 04:07
  • Does this answer your question? [Why does cls.\_\_name\_\_ not appear in dir()?](https://stackoverflow.com/questions/7096132/why-does-cls-name-not-appear-in-dir) Specifically [this answer](https://stackoverflow.com/a/45635523/4518341) – wjandrea May 29 '21 at 20:39
  • @wjandrea wow! exactly right. thank you!!! – gangmin.chun May 30 '21 at 22:17

0 Answers0