Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dynamically define class's
#1
Python forum


I am looking for a way to dynamically define class's.


my motivation is to define a material library for my company that will be updated through excel periodically by other people. the library will be changing, such that available materials will be different, prices will be different, properties will change,....


I want my objects to reflect the updated version of this database.

I want to import the excel database using pandas data frame, and then generate class's with attributes driven from the database.

there is alot of information regarding metaclass's or how to define class's using the type function, however this won't help me, because the init function must still be fully defined out of the type function class generation statment.

this doesn't really help me because in addition I don't know how to dynamically define functions.

its a bit weird, the internet considers this topic isoteric, like its just a deep pythonism that isn't practical, however, I don't understand why everyone doesn't program like this.

anyways, please help,
regards,
Reply
#2
(Feb-26-2019, 08:17 AM)michavardy Wrote: I don't understand why everyone doesn't program like this.
You show basic misunderstanding of OOP and programming principles in general. You should always separate logic from data. In your case you would define class for different materials with respective properties. There may be several different classes if materials have significantly different properties or they may inherit from one another if they share certain properties.
So based on the data (e.g. price,quantity, dimensions, etc.) you may instantiate many instances of given class.
In other words you don't want to define another class if for example price changes.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dynamically setting nested class be_ams 0 1,515 Apr-06-2022, 02:09 PM
Last Post: be_ams
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 2,035 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  Help creating a class instance dynamically Kotevski 9 5,374 Aug-17-2018, 05:23 AM
Last Post: Gribouillis
  how do you define Class in python ? volcano 6 4,328 Apr-09-2018, 07:24 PM
Last Post: nilamo
  Import class dynamically voltron 4 4,776 Feb-05-2018, 01:24 PM
Last Post: voltron

Forum Jump:

User Panel Messages

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