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
#2
Did you check the docs:
https://docs.python.org/3/library/functions.html#type

Quote: class type(object)
class type(name, bases, dict, **kwds)

With one argument, return the type of an object. The return value is a type object and generally the same object as returned by object.__class__.

The isinstance() built-in function is recommended for testing the type of an object, because it takes subclasses into account.

With three arguments, return a new type object. This is essentially a dynamic form of the class statement. The name string is the class name and becomes the __name__ attribute. The bases tuple contains the base classes and becomes the __bases__ attribute; if empty, object, the ultimate base of all classes, is added. The dict dictionary contains attribute and method definitions for the class body; it may be copied or wrapped before becoming the __dict__ attribute. The following two statements create identical type objects:

class X:
    a = 1

X = type('X', (), dict(a=1))
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
#3
(Apr-19-2024, 10:56 AM)Pedroski55 Wrote: I don't understand classes in Python!

Why do I get a class like this, without declaring a class??
Every object in python is an instance of some class. This applies to classes. Normally, classes are instances of the type type
>>> class A:
...     pass
... 
>>> type(A)
<class 'type'>
Well is type an instance of something else?
>>> type(type)
<class 'type'>
>>> isinstance(type, type)
True
>>> 
type is an instance of itself.

Can classes be instances of something else than type?

YES! this is called a metaclass.
>>> class MetaSpam(type):
...     pass
... 
>>> class Spam(metaclass=MetaSpam):
...     pass
... 
>>> 
>>> type(Spam)
<class '__main__.MetaSpam'>
The class Spam is an instance of MetaSpam, a derived type of type.
« We can solve any problem by introducing an extra level of indirection »
Reply
#4
Thanks for the replies.

I have no theoretical knowledge of computing. I am mainly interested in what practical use anything can be, in order to make a given task easier and faster.

Not sure how querying the type of type, or knowing that a class is an instance of type helps. Why do we need class in that case?

I thought type was only for querying what kind of variable you had, now, using type I can create a class and go on create instances of that class (=type)!

Quote:Now what can we do with instance objects? The only operations understood by instance objects are attribute references. There are two kinds of valid attribute names: data attributes and methods.
Reply
#5
(Apr-19-2024, 04:38 PM)Pedroski55 Wrote: I am mainly interested in what practical use anything can be, in order to make a given task easier and faster.
Having a consistent language makes the task of programming that language easier and faster. That's why it is good design that classes in Python are first class objects like any other object. For example it allows programmers to pass classes as arguments to function or to make collections of classes etc. Not all languages allow that.

Perhaps you don't use these features directly in your code because you're not interested in metaprogramming and other advanced features that Python allows. If you ever use introspection or serialization or packaging etc, you'll be using these advanced features through libraries that are built on them.
(Apr-19-2024, 04:38 PM)Pedroski55 Wrote: I thought type was only for querying what kind of variable you had, now, using type I can create a class and go on create instances of that class (=type)!
When you have a class A, you can use A() as a function to create instances of A objects. In the same way you can use type() to create instances of type object, that is to say classes.

Class definitions are a syntactic facility to ease the creation of most classes. The situation is similar with the dictionary syntax, you can create a dictionary functionally with the dict() creator or literally with the dictionary syntax
# functionally
d = dict(spam=1, eggs=2)

#literally:
d = { 'spam': 1, 'eggs': 2 }
For classes
# functionally
A = type('A', (), {})

#literally
class A:
    pass
Having a functional way of doing things can be more suitable for classes that are automatically generated by a program instead of being hard coded.
« We can solve any problem by introducing an extra level of indirection »
Reply
#6
Thanks again, I appreciate it.

I only do simple things, I never felt the need to create a class, so I never delved into them.

Quote:When you have a class A, you can use A() as a function to create instances of A objects.

Now you are confusing me, but I am often confused, doesn't matter!
Reply
#7
(Apr-21-2024, 05:08 AM)Pedroski55 Wrote: Now you are confusing me, but I am often confused, doesn't matter!
Yes, the word function is misleading. The accurate word for this is callable. A class is callable although it is not a function.

As an example of overuse of the class syntax, see this snippet where I create dictionaries with the class syntax instead of creating classes, so instead of the above code to create a dict, I can write
>>> class d(metaclass=just_a_dict):
...     spam = 1
...     eggs = 2
... 
>>> d
{'spam': 1, 'eggs': 2}  # d is a dictionary, not a class
This is crazy code, but it works.
« We can solve any problem by introducing an extra level of indirection »
Reply


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,699 Jan-20-2022, 05:15 PM
Last Post: deanhystad
  Create Generator in the Class quest 1 2,189 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,193 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,108 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,101 May-29-2020, 12:30 PM
Last Post: KM007
  How I can create reference to member of the class instance? AlekseyPython 8 3,967 Dec-05-2018, 06:24 AM
Last Post: AlekseyPython
  Help writing code to create a ranked list swilson421 2 2,521 Jul-16-2018, 04:51 PM
Last Post: swilson421
  problem with simple class code diegoraffo 5 3,767 Jan-27-2018, 02:31 AM
Last Post: ka06059
  Trying to create a child class in Python 2.7 mac77 1 2,965 Sep-27-2017, 06:15 PM
Last Post: nilamo
  Create class instances from list of strings pythonck 1 3,679 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