Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Creating Dynamic Objects
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):
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, 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.

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
>>> s.testing
>>> s.test = 'foo'
>>> s
Sns(test='foo', testing='eggs', ing='bacon')
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" an example.
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?
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)

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  An Object of Objects? cebb 4 198 Jul-28-2019, 03:29 PM
Last Post: cebb
  rotating and resizing objects jenya56 3 155 Jul-26-2019, 12:06 AM
Last Post: scidam
  Help | How to make Dynamic Path/Address soothsayerpg 3 180 Jul-14-2019, 03:51 PM
Last Post: DeaD_EyE
  Dynamic function name bhojendra 5 220 Jul-12-2019, 12:17 AM
Last Post: scidam
  Does XLRD have a method to extract embedded objects (pdf) from an excel sheet? matthewme 1 190 Jun-13-2019, 10:00 PM
Last Post: Larz60+
  building a dynamic INSERT between two DBs with Python3 liran 5 271 May-30-2019, 07:38 AM
Last Post: heiner55
  multi-dimm array -- dynamic PappaBear 3 239 May-03-2019, 03:47 AM
Last Post: buran
  Moving with objects simultaneously kom2 1 199 Apr-20-2019, 07:12 PM
Last Post: SheeppOSU
  Generating number of objects for a game kom2 3 252 Apr-18-2019, 02:04 PM
Last Post: dan789
  Creation of Dynamic HTML by substituting Database values Sandy777 1 227 Apr-18-2019, 07:17 AM
Last Post: buran

Forum Jump:

Users browsing this thread: 1 Guest(s)