Python Forum
Getting attributes from a list containing classes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting attributes from a list containing classes
#1
Hi!

I have a small problem:

I defined a class Tone which inherits from the class Recorder. (i.e. a recorder is an instrument which has many notes). Each Tone has certain attributes like frequency, fingering, etc. Currently, I am doing the following:

class Recorder(object):
    def __init__(self):
        self.alltones=[Tone(ii) for ii in range(10)]
class Tone(Recorder):
    def __init__(self,ii):
        self.frequency=2**(ii/12)


This however doesn't satisfy me as I would like to be able to easily create a list of all frequencies from an instance of Recorder. Something along these lines

a=Recorder()
a.alltones.frequency
In particular, I am looking for a clean way of creating lists of the following type
[Tone(ii).attribute for ii in range(10)]
Thank you in advance for your help :)
Reply


Messages In This Thread
Getting attributes from a list containing classes - by midarq - Dec-02-2019, 06:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [solved] Classes, assign an attributes to a class not to instances.. SpongeB0B 4 925 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  Listing Attributes of Objects & Classes JoeDainton123 4 2,347 Aug-28-2020, 05:27 AM
Last Post: ndc85430
  how to add class instance attributes from list 999masks 2 2,699 Jul-22-2019, 07:59 AM
Last Post: 999masks
  Copying classes in a list marienbad 1 2,633 Oct-14-2018, 04:42 PM
Last Post: ichabod801
  Classes: How to Print Specific Attributes emerger 3 3,949 Oct-13-2018, 08:49 PM
Last Post: emerger
  Using classes? Can I just use classes to structure code? muteboy 5 5,026 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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