Understanding Object Type in Python

In summary, the conversation discusses the concept of object type in Python and its relationship to data types and classes. It is clarified that "type" and "class" are not interchangeable concepts and the Python documentation provides more information on this topic. It is also noted that the function to get the name of the class of an object is called "type," which can be confusing. Additionally, the use of different terms such as "numbers" and "numeric types" may cause confusion, but they refer to the same concept.
  • #1
Arman777
Insights Author
Gold Member
2,168
193
Adsız.png


As far as I know, there is no object type in python. How it's determined?

For instance, we know that 2 is an object and it belongs to the int class. Similarly, 2.1 is an object, and it belongs to the float class. From my research, I have learned that data type ##\equiv## class. Then what the heck is object type? Is it just a personal naming convention done by the author?
 
Technology news on Phys.org
  • #2
  • Like
Likes Vanadium 50 and Arman777
  • #3
pbuk said:
No, you can check this by RTFM: https://docs.python.org/3/library/stdtypes.html.
I see but is there way to reach them via python ? For instance I can reach the class (data type) via type(). Is there a way to reach object type (numeric, string etc,) via a command ?

I think there's not but just wanted to ask.
 
  • #4
Arman777 said:
From my research, I have learned that data type = class. Then what the heck is object type?
In general, "type" to a much wider concept than "class". Put very short you could say "class" corresponds to "custom type", i.e. a type with custom semantics defined by the program as opposed being a built-in type in the language in question with a "fixed" syntax and semantics.

In Python3, the internal implementation of the "type" and "class" has been unified (I am not into the details of that), but that does not mean you should think of them as being the same concept when you write your own python program.
 
  • #5
Arman777 said:
I see but is there way to reach them via python ? For instance I can reach the class (data type) via type(). Is there a way to reach object type (numeric, string etc,) via a command ?
Of course not. You can see from the manual that 'type' is a concept.
Python manual said:
The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions.
How can a command return a concept?

In your defence, it doesn't help that the function to get the name of the class of an object is type, but if the language was completely logical they wouldn't have called it Python :biggrin:
 
Last edited:
  • Haha
Likes Vanadium 50
  • #6
In the book it says numbers, string, list etc. However in the article it says Numeric, Sequence Types etc. Thats also strange
 
  • #7
That's because they are concepts: 'numbers' and 'numeric types' are the same concept.
 

1. What is an object in Python?

An object in Python is a piece of data that has both attributes (characteristics) and methods (actions). Objects can be created from classes, which act as templates for creating objects with similar characteristics and behaviors.

2. How do you create an object in Python?

To create an object in Python, you first need to define a class with the desired attributes and methods. Then, you can use the class name followed by parentheses to create an instance of that class, which is an object. For example, if you have a class called "Car", you can create an object called "my_car" by using the syntax "my_car = Car()".

3. What is the difference between a class and an object?

A class is a template for creating objects, while an object is an instance of a class. In other words, a class defines the characteristics and behaviors that an object will have, while an object is a specific instance of those characteristics and behaviors.

4. How do you access attributes and methods of an object?

To access attributes and methods of an object, you use dot notation, which involves using the object name followed by a dot and then the attribute or method name. For example, if you have an object called "my_car" with an attribute called "color", you can access it by using "my_car.color". Similarly, if you have a method called "drive" in the "Car" class, you can access it by using "my_car.drive()".

5. Can you change the attributes of an object?

Yes, you can change the attributes of an object. Since attributes are simply variables stored within an object, you can change them by reassigning them to a new value. However, if the attribute is defined as a constant (using the "const" keyword), it cannot be changed.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
10
Views
4K
  • Programming and Computer Science
Replies
25
Views
3K
  • Programming and Computer Science
Replies
23
Views
2K
Replies
3
Views
766
  • Programming and Computer Science
Replies
24
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top