Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating Dynamic Objects
#1
Hi everyone,

Rather new to python and have had no luck in researching this, so assuming the answer is no, but here is what I am trying to see if is possible:

Have an object that has open attributes(possible), have an array(...obviously possible). Now the tricky part, name the attributes based off of the names in the array.

So my example, which doesn't work at all, but I hope get's the idea across if it is possible, is below:

fields = ["test","testing","ing"]
Class sns(object):
    pass
i = 0
obj = sns()
while i < len(fields):
    obj.fields[i] = "data"
    i += 1
With the expected results, being the object "obj", with the properties/attributes obj.test, obj.testing, obj.ing. The idea is that I'd like to take something like a csv file, dynamically take the first row, and create objects with attributes named after that first row, if that makes any sense.

Anyhow, thanks in advance.

Cheers,
Mac
Reply
#2
You can use built-in collections.namedtuple objects to create immutable records with named fields. If you prefer the mutable version, there is one in the module namedlist in pypi
>>> from namedlist import namedlist
>>> Sns = namedlist('Sns', ['test', 'testing', 'ing'], default=None)
>>> s = Sns('spam', 'eggs', 'bacon')
>>> s
Sns(test='spam', testing='eggs', ing='bacon')
>>> s.test
'spam'
>>> s.testing
'eggs'
>>> s.test = 'foo'
>>> s
Sns(test='foo', testing='eggs', ing='bacon')
Reply
#3
Thanks for the response, but I'm not sure how this could work with a dynamic number of fields per my end statement. The idea is to have the same code be able to create an object from different files, when one might have the headers: "Computer", "SN", "Asset Tag"; and another have the headers: "User Name", "Location", "Job Title", "Status"...as an example.
Reply
#4
Quote:I'm not sure how this could work with a dynamic number of fields per my end statement.
I cannot see how it could fail to work with a dynamic number of fields. Can you post some code?
Reply
#5
Well I guess that's the problem...I have no idea how to go about it. Here's the theory:
fields = ['test','test','ing']
#where fields can be any anything, any number, any text, these are just place holders as an example
from namedlist import namedlist
Sns = namedlist('Sns',for f in fields:[f], default=None)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help with creating dynamic columns with for loops for stock prices PaDat 2 858 Feb-22-2023, 04:34 AM
Last Post: PaDat
  Creating list of lists, with objects from lists sgrinderud 7 1,561 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,430 Jul-27-2022, 08:50 PM
Last Post: rob101
  error creating new object after loading pickled objects from file arogers 2 3,366 Feb-02-2019, 10:43 AM
Last Post: Larz60+
  Creating a list of class objects hjuyrfc 2 3,029 Jul-22-2017, 07:41 PM
Last Post: hjuyrfc
  Creating Dynamic Variable Names Dragonexpert 3 8,071 Oct-22-2016, 02:17 PM
Last Post: Dragonexpert

Forum Jump:

User Panel Messages

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