#NoStupidQuestion: In Python, what exactly is “list of valid attributes of an object”?

anjanj
3 min read

Imagine an object like a box. Inside that box, you can have stuff — like labels (name, age) or tools (things the box can do, like to greet you).

These “labels” and “tools” are what we call the object’s attributes.

Example:

class Person:
    def __init__(self, name):
        self.name = name

    def say_hello(self):
        print("Hi there!")

person = Person("Alice")

Here’s what’s happening:

  • person.name is an attribute → it holds the value "Alice"
  • person.say_hello() is also an attribute → it’s something the object can do
  • If you try something random like person.favorite_color and it’s not defined? ❌ Not a valid attribute.

So when we say “list of valid attributes of an object”, we are referring to all the names that we can access using dot notation on that object.

These include:

1. Instance Variables

These are attributes defined in the object itself (usually in init):

class Person:
    def __init__(self, name):
        self.name = name

p = Person("Alice")
p.name  # 'name' is an instance variable

2. Methods

Functions defined in the class become attributes of the object and are callable:

class Person:
    def greet(self):
        return "Hello"

p = Person()
p.greet()  # 'greet' is a method

3. Inherited Attributes

Anything defined in a parent class or built into Python:

p.__class__  # comes from Python's base 'object' class

4. Magic / Dunder Methods

These are special names like init, str, etc. Used by Python internally.


To get the list of valid attributes of an object in Python, you can use the built-in function dir():

attributes = dir(your_object)
print(attributes)

dir(your_object) gives us a list of all these attributes in a single list — both visible (like .name) and behind-the-scenes (like .__class__, .greet()).

The list you get from dir() will contain everything that the object “knows about” — including:

1. Variables (Attributes)

These are values stored in the object.

For example:

person.name = "Alice"

• name is a variable attached to the object — it’s one of the attributes.

• You’ll see "name" in the list from dir(person).


2. Methods (Callables)

These are things the object can do — functions inside the object.

For example:

person.say_hello()

• say_hello is a method — basically a function attached to the object.

• You’ll also see "say_hello" in the list from dir(person).


3. Internal System-Level Attributes (aka “dunder” methods)

These are special things Python uses behind the scenes.

They have names like class, init, str, etc.

You normally don’t touch these as a beginner, but they’re included too.

Example:

print(person.__class__)

• This tells you what kind of object it is.

• It’s included in the list from dir(person), even though you didn’t write it yourself.


📦 Think of dir() like opening a toolbox

When you run dir(person), it’s like opening a toolbox and seeing:

• 🔧 Tools you put in there (like say_hello)

• 🏷️ Labels you added (like name)

• ⚙️ Hidden tools Python adds automatically (like class, init)

To conclude, “valid attributes” = all names we can access with obj.<name> without getting an error.

0
Subscribe to my newsletter

Read articles from anj directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

anj
anj