Python Forum
How does this code create a class?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How does this code create a class?
#1
I don't understand classes in Python!

Why do I get a class like this, without declaring a class??

Employee = type("Employee", (object,), dict())
Enter Employee in Idle:

Employee

Output:
<class '__main__.Employee'>
Make an instance of Employee:

employee = Employee()

# Set salary to 1000
setattr(employee,"salary", 1000 )

# Get the Salary
value = getattr(employee, "salary")
print(value) # returns 1000
employee.salary # returns 1000
Reply


Messages In This Thread
How does this code create a class? - by Pedroski55 - Apr-19-2024, 10:56 AM
RE: How does this code create a class? - by buran - Apr-19-2024, 11:14 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Cannot convert the series to <class 'int'> when trying to create new dataframe column Mark17 3 8,657 Jan-20-2022, 05:15 PM
Last Post: deanhystad
  Create Generator in the Class quest 1 2,171 Apr-15-2021, 03:30 PM
Last Post: jefsummers
  Class function is not running at all even though it is clearly run in the code SheeppOSU 2 2,171 Sep-26-2020, 10:54 PM
Last Post: SheeppOSU
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 2,089 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  How to create a varying qty of instances of a Class and name them? KM007 2 2,088 May-29-2020, 12:30 PM
Last Post: KM007
  How I can create reference to member of the class instance? AlekseyPython 8 3,945 Dec-05-2018, 06:24 AM
Last Post: AlekseyPython
  Help writing code to create a ranked list swilson421 2 2,499 Jul-16-2018, 04:51 PM
Last Post: swilson421
  problem with simple class code diegoraffo 5 3,745 Jan-27-2018, 02:31 AM
Last Post: ka06059
  Trying to create a child class in Python 2.7 mac77 1 2,949 Sep-27-2017, 06:15 PM
Last Post: nilamo
  Create class instances from list of strings pythonck 1 3,658 Sep-18-2017, 06:13 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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