Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8403791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:26:12+00:00 2026-06-09T22:26:12+00:00

My question is how to create variables "on the fly". I’m trying to generate

  • 0

My question is how to create variables "on the fly". I’m trying to generate an object with a random set of attributes.

from random import randint, choice

class Person(object):
    def __init__(self):
      self.attributes = []    

possible_attributes= ['small', 'black', 'scary', 'smelly', 'happy'] # idk, random
chance = randint(1,5)

chosen_attributes = []
for i in xrange(chance):
    #psuedo code...
    local_var = choice(possible_attributes)
    if local_var not in chosen_attributes:
        VAR = local_var # VAR needs to be dynamic and 'global' for use later on
        chosen_attributes.append(local_var)
        Person.attributes.append(local_var)

I’m positive how I’m wanting to do this is not a good way, so if anyone understand what I’m looking for and can offer a better method (one that works, for starters) I would be most appreciative.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:26:13+00:00Added an answer on June 9, 2026 at 10:26 pm

    To add attributes to an instance of the class you can use .__dict__:

    class Person(object):
        def addattr(self,x,val):
            self.__dict__[x]=val
    

    output:

    >>> a=Person()
    >>> a.addattr('foo',10)
    >>> a.addattr('bar',20)
    >>> a.foo
    10
    >>> a.bar
    20
    >>> b=Person()
    >>> b.addattr('spam','somevalue')
    >>> b.spam
    'somevalue'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has been edited for clarification: I am trying to create "pretty URLs"
I am trying to create an anonymous function but need to access variables from
EDIT: Simple version of the question: I want to create server variables in the
i got a question when create a javascript object, when one function invoking another
I am trying to create a question and answers page. In which I am
Question I'm trying to create a basic wxWidgets program with a text entry box,
This is a continuation from a previous stackoverflow question. I've renamed some variables so
I have implemented remember me functionality as this question How to create "remember me
I have a small question. I'm trying to create templates for getters for my
One question : How do you dynamically draw to a Tkinter Canvas object from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.