>>> class new_class():
...   def __init__(self, number):
...     self.multi = int(number) * 2
...     self.str = str(number)
... 
>>> a = new_class(2)
>>> a.__dict__
{'multi': 4, 'str': '2'}
>>> a.__dict__.keys()
dict_keys(['multi', 'str'])

You may also find pprint helpful.

Answer from Roger Pate on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-get-a-list-of-class-attributes-in-python
How to Get a List of Class Attributes in Python? - GeeksforGeeks
July 12, 2025 - Another way of finding a list of attributes is by using the module inspect. This module provides a method called getmembers() that returns a list of class attributes and methods.
People also ask

What happens if both instance attribute and class attribute are defined?

In that case, the instance namespace takes precedence over the class namespace. If there is an attribute with the same name in both, the instance namespace will be checked first and its value returned.

🌐
toptal.com
toptal.com › python › python-class-attributes-an-overly-thorough-guide
Python Class Attributes: An Overly Thorough Guide | Toptal®
What is a Python namespace?

A Python namespace is a mapping from names to objects, with the property that there is zero relation between names in different namespaces. Namespaces are usually implemented as Python dictionaries, although this is abstracted away.

🌐
toptal.com
toptal.com › python › python-class-attributes-an-overly-thorough-guide
Python Class Attributes: An Overly Thorough Guide | Toptal®
Python class method versus instance method: What’s the difference?

In Python, a class method is a method that is invoked with the class as the context. This is often called a static method in other programming languages. An instance method, on the other hand, is invoked with an instance as the context.

🌐
toptal.com
toptal.com › python › python-class-attributes-an-overly-thorough-guide
Python Class Attributes: An Overly Thorough Guide | Toptal®
🌐
Mouse Vs Python
blog.pythonlibrary.org › home › how to get a list of class attributes in python
How to Get a List of Class Attributes in Python - Mouse Vs Python
January 31, 2020 - The first method that we will look at is using Python’s inspect module. import inspect variables = [i for i in dir(t) if not inspect.ismethod(i)] Doesn’t look too complicated, does it? But it requires an import and I’d prefer not to do that. On the other hand, if you need to do introspection, the inspect module is a great way to go. It’s quite powerful and can tell you lots of wonderful things about your class or one you didn’t even write.
🌐
Toptal
toptal.com › python › python-class-attributes-an-overly-thorough-guide
Python Class Attributes: An Overly Thorough Guide | Toptal®
March 5, 2014 - Python class attributes can lead to elegant code—as well as bugs. This guide outlines use cases for attributes, properties, variables, objects, and more.
🌐
Python Tutorial
pythontutorial.net › home › python oop › python class attributes
Understanding Python Class Attributes By Practical Examples
November 13, 2020 - When you access an attribute via an instance of the class, Python searches for the attribute in the instance attribute list. If the instance attribute list doesn’t have that attribute, Python continues looking up the attribute in the class attribute list.
🌐
Net Informations
net-informations.com › python › iq › attributes.htm
How to Get a List of Class Attributes in Python
class MyClass: class_attr1 = 42 ... {'__module__': '__main__', 'class_attr1': 42, 'class_attr2': 'Hello', ...} The dir() function returns a list of valid attributes and methods for an object....
🌐
CodeSpeedy
codespeedy.com › home › get a list of class attributes in python
Get a List of Class Attributes in Python - CodeSpeedy
January 9, 2022 - It returns a list of the attributes and methods of the passed object/class. On being called upon class objects, it returns a list of names of all the valid attributes and base attributes too.
Find elsewhere
🌐
Tutorialspoint
tutorialspoint.com › python › python_class_attributes.htm
Python - Class Attributes
__bases__ − A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list. To access built-in class attributes in Python, we use the class name followed by a dot (.) and then attribute name.
🌐
Python documentation
docs.python.org › 3 › tutorial › classes.html
9. Classes — Python 3.14.3 documentation
Attribute references use the standard syntax used for all attribute references in Python: obj.name. Valid attribute names are all the names that were in the class’s namespace when the class object was created.
🌐
Reddit
reddit.com › r/learnpython › setting list as a class attribute
r/learnpython on Reddit: Setting list as a class attribute
March 11, 2024 -

Hi!

I just finished a turtle crossing ( a.k.a. Frogger) game as part of an online bootcamp. It uses the turtle library.

I managed quite easily, but there is one point which I can't really wrap my head around.

So I am generating Car class objects in random lanes. In order to be able to move them, query them in loops, etc. One of the init attributes is

self.cars = [ ]

Hence adding them to a list where I can apply different methods on them.

I remembered this from a previous lesson (snake game) but I still don't fully understand how an attribute can be a list.

Can someone explain this to a beginner?

🌐
Reddit
reddit.com › r/learnpython › how to access attributes of a class in a list that was made my iteration?
r/learnpython on Reddit: How to access attributes of a class in a list that was made my iteration?
July 24, 2023 -

I've had a lot of issues getting information into and out of a list of classes that I've generated by iteration. In my latest project, I'm using PDF2image to open a pdf and store each page in a list of classes as well as a few other attributes unique to each page. My problem is that I have a really loose grip on how to handle data coming in and out of functions and particularly classes. Like in the following:

'class page():

 def __init__(self, image, other_stuff)

      Self. Image = image

      Self. Other_stuff = other

Page_list = []

def pdfloader():

 Pages = convert_from_bytes(path)

 For page in pages:

      Page_list.append(page(image = page, other_stuff = None))

'

So like, in this example, I've appended 'page_list' with a new instance for each page that was pulled from the pdf and stored as an array. This works great. But now I have no idea how to access each instance to get the array, and then write 'other_stuff' to that instance.

(Also, sorry for the formatting, I'm writing this on mobile and I have to be in bed for work tomorrow. If it makes no sense, I'll take it down and re-post when I get the chance on desktop)

🌐
Built In
builtin.com › software-engineering-perspectives › python-attributes
Python Attributes: Class vs. Instance | Built In
Summary: Python class attributes are shared across all instances, while instance attributes are unique to each object. Attribute lookup starts in the instance’s namespace, then checks the class. Reassigning a class attribute from an instance creates a new instance attribute that shadows the original.
🌐
Quora
quora.com › What-is-the-best-way-to-list-only-the-class-attributes-of-a-class-in-Python
What is the best way to list only the class attributes of a class in Python? - Quora
Answer (1 of 3): You can use vars(), which will return a dictionary with the class attributes. [code] class MyClass( ): def __init__(self): self.foo = 9 self.bar = 10 def class_attrs(self): return vars(self) c = MyClass() c.class_attrs() # ==> {'foo': 9, 'bar': 10} [/code] ED...
🌐
Mooc
programming-21.mooc.fi › part-9 › 2-objects-as-attributes
Objects as attributes - Python Programming MOOC 2021
In object oriented programming it is often the case that we want to have a collection of objects as an attribute. For example, the relationship between a sports team and its players follows this pattern: class Player: def __init__(self, name: str, goals: int): self.name = name self.goals = goals def __str__(self): return f"{self.name} ({self.goals} goals)" class Team: def __init__(self, name: str): self.name = name self.players = [] def add_player(self, player: Player): self.players.append(player) def summary(self): goals = [] for player in self.players: goals.append(player.goals) print("Team:", self.name) print("Players:", len(self.players)) print("Goals scored by each player:", goals)
🌐
Python.org
discuss.python.org › python help
Accessing attributes of a class - Python Help - Discussions on Python.org
February 22, 2022 - When I do this, class C: pass dir(C) then it gives me, ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', ...
Top answer
1 of 3
1

This should be closer to what you want:

class Crime(object):
    def __init__(self, id_jenis, jenis):
        self.id_jenis=id_jenis
        self.jenis=jenis

class DistanceNeighbor(object):
    def __init__(self, distance, crimes):
        self.distance = distance
        self.crimes = crimes


data_distance = []
for id_objek1, objek1, id_objek2, objek2, distance in cur.fetchall():
    crimes = [Crime(id_objek1,objek1), Crime(id_objek2,objek2)]
    data_distance.append(DistanceNeighbor(distance, crimes))

Classes in Python 2 should always inherit from object. By convention, class names are in CamelCase.

The inheritance of DistanceNeighbor from Crime seems unnecessary. I changed this.

Attributes to instance should be lower case, therefore I used crimes instead of the very confusing reuse of the class name Crime.

This line:

def __init__(self, distance, **Crime):

takes your list of Crime instance apart as separate arguments. In your case it means the __init__ receives:

distance, data_Crime[0], data_Crime[0]

this causes this error message:

TypeError: init() takes exactly 2 arguments (3 given)

The instantiation of Crime is pretty short. So, instead of the two appends you can create the list of the two Crime instances in one line:

crimes = [Crime(id_objek1,objek1), Crime(id_objek2,objek2)]

Since this creates a new list in each loop, there is no need to delete the list content in each loop, as you did with del data_Crime[:].

2 of 3
1

You've defined your __init__ method in distance_neighbor as taking arguments (self, distance, **Crime). The ** before Crime tells Python to pack up any keyword arguments you're passed into a dictionary named Crime. That's not what you're doing though. Your call is distance_neighbor(distance, data_Crime) where data_Crime is a list. You should just accept that as a normal argument in the __init__ method:

class distance_neighbor (Crime):
   def __init__(self, distance, crime):
      self.distance = distance
      self.crime = crime

This will mostly work, but you'll still have an issue. The problem is that the loop that's creating the distance_neighbor objects is reusing the same list for all of them (and using del data_Crime[:] to clear the values in between). If you are keeping a reference to the same list in the objects, they'll all end up with references to that same list (which will be empty) at the end of the loop.

Instead, you should create a new list for each iteration of your loop:

for id_objek1, objek1, id_objek2, objek2, distance in cur.fetchall():
    data_Crime = [Crime(id_objek1,objek1), Crime(id_objek2,objek2)]
    data_distance.append(distance_neighbor(distance, data_Crime))

This will work, but there are still more things that you probably want to improve in your code. To start with, distance_neighbor is defined as inheriting from Crime, but that doesn't seem appropiate since it contains instance of Crime, rather than being one itself. It should probably inherit from object (or nothing if you're in Python 3 where object is the default base). You may also want to change your class and variable names to match Python convention: CamelCase for class names and lower_case_with_underscores for functions, variables and attributes.

🌐
freeCodeCamp
freecodecamp.org › news › python-attributes-class-and-instance-attribute-examples
Python Attributes – Class and Instance Attribute Examples
April 12, 2022 - When creating a class in Python, you'll usually create attributes that may be shared across every object of a class or attributes that will be unique to each object of the class. In this article, we'll see the difference between class attributes and ...
🌐
Turing
turing.com › kb › introduction-to-python-class-attributes
A Guide to Python Class Attributes and Class Methods
Python's classes and objects allow you to design your code in a way that is intuitive and easy to understand. This article will take you through Python class attributes, their purpose and how they’re used as well as class methods and how to create them.
🌐
Tutorial Teacher
tutorialsteacher.com › articles › class-attributes-vs-instance-attributes-in-python
Class attributes vs instance attributes in Python
Instance attributes are attributes or properties attached to an instance of a class. Instance attributes are defined in the constructor. The following table lists the difference between class attribute and instance attribute: