0

Let's say I have following code:

mystring='abcd'
print(type(mystring))

Python understands that mystring is a string object and returns <class 'str'>. How does this happen? What if I want to declare my own data type that supports the same logic, that is when I type print(type(mystring)) I receive <class 'myClass'>, not python default class <class 'str'>. How can I achieve that?

Update: I am interested in how to rearrange the logic of class auto-definition? I don't want to declare variable explicitly as an object of myClass by myString = myClass('abcd'). I just want to type myString = 'abcd' and be sure that Python has defined myString as instance of myClass. Is that possible? May be it is all about inheritance/overriding str class?

5
  • 1
    What do you mean, how does this happen? You have a string and you got its type, and its type is str, what else would happen? Commented Jun 13, 2021 at 16:52
  • To define a custom type you define a class. Indeed, "class" and "type" in python are synonymous. Indeed, type is just a class, the class of all classes, i.e. a metaclass if you want to know a little more about how it all works Commented Jun 13, 2021 at 18:14
  • Beside the point, but class names should be UpperCamelCase, so, MyClass. Commented Jun 13, 2021 at 18:17
  • 'abcd' is always in instance of str, you can't make it something else without being explicit about it. Commented Jun 13, 2021 at 18:17
  • For reference: Lexical analysis > Literals > String and Bytes literals Commented Jun 13, 2021 at 18:20

2 Answers 2

1

You can't do what you want to do. How literals are defined in Python is inherent to the language. You can definitely override str which will change what happens when it's actually used, but this isn't recommended.

If you could globally overwrite the inherent primitive types of the language how would you expect other packages to behave?

>>> class MyClass:
...     def __init__(self, string):
...         self.strlen = len(string)
...

>>> str = MyClass

>>> type("foo")
str

>>> type(MyClass("foo"))
__main__.MyClass

>>> type(str("foo"))
__main__.MyClass
Sign up to request clarification or add additional context in comments.

Comments

0

You are talking about Object Oriented Programming in python. You can make your own custom class with specific functions.

For example:

class myClass(str):
    pass

myString = myClass('abcd')
print(type(myString))

Output:

<class '__main__.myClass'>

3 Comments

Thank you for your answer but my question concerns a little bit different issue: I am interested in how to rearrange the logic of class auto-definition? I don't want to declare variable explicitly as an object of myClass by myString = myClass('abcd'). I just want to type myString = 'abcd' and be sure that Python has defined myString as instance of myClass. Is that possible? May be it is all about inheritance/overrideinf str class?
@KiNest what you are asking for doesn't make any sense. Python doesn't have "class auto-definition". You create objects in Python, and any object has a type. myString = myClass('abcd') is not a declaration, python doesn't have variable declarations.
@KiNest and note, you probably almost certainly do not want to inherit from str. Maybe, but I suspect not

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.