How to cast in python | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 2

How to cast in python

Hi I have a doubt on casting in python. Refer below code. How can I cast for test1 to test? a = 1.3 b = int(a) Works fine, but it is as both are compatible. In my case, test1 and test are not compatible , but it should give some error related to incompatible type rather than argument errors. Right? Or am I missing something? https://sololearn.com/compiler-playground/cPSVLCwhI2Pn/?ref=app

15th May 2024, 2:49 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
14 Réponses
+ 3
How can we speak in terms of casting if python is not a strongly-typed language like Java etc? It makes no sense to me.
16th May 2024, 6:47 AM
Oma Falk
Oma Falk - avatar
+ 2
No, this will give an argument error. The test __init__ method doesn’t take any arguments, let alone an instance of test11. test11 isn’t quite “incompatible” with test per se as there’s no way for them to interact in the first place.
15th May 2024, 3:13 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 2
Casting in python is not possible(necessary). A variable in Python can change its type. A="test" A=3 Is possible.
15th May 2024, 3:56 PM
Oma Falk
Oma Falk - avatar
+ 2
If you want a new instance of Class 2 to use data from an instance of Class 1, you must set up Class 2’s __init__ method to accept an instance of Class 1 as an argument and set Class 2’s attributes based on it. It might help if you said exactly what you were trying to do. You may have condensed the question incorrectly.
15th May 2024, 5:04 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 2
Ketan Lalcheta Building on Wilbur Jaywright's comment, you can define casting functions. Don't mix in inheritance, but the classes have to be compatible, though, so that the attributes can be passed on. Without the inheritance complication, casting becomes just a simple re-instancing to a different class and passing and possibly recasting the attributes. https://sololearn.com/compiler-playground/civfWz7riwrT/?ref=app
16th May 2024, 12:30 AM
Bob_Li
Bob_Li - avatar
+ 2
Oma Falk yes, Python's dynamic typing makes it really easy to switch classes. So just re-initialze the variable as something else. Ketan Lalcheta 's condition of not wanting to reinstantiate with __init__ is a hard obstacle, since passing by value is the default mode of getting things done in Python.
16th May 2024, 8:03 AM
Bob_Li
Bob_Li - avatar
+ 2
Bob_Li yeah but thats a mantra of OOP. We need __init__ to get the initial state. All called methods rely on it. ABAP (SAP-language) can do it: A variable with type superclass can be assigned to inhereted class. But you MUST declare that you know what you do. Normally this is a strong hint for a bad style.... and you dont know what you do :-) On the other hand if class B inherits from classA: a B-typed variable can can be assigned to a variable of Class A. Which is finally polymorphism.
16th May 2024, 8:56 AM
Oma Falk
Oma Falk - avatar
+ 1
It gives an argument error, since you are giving “arguments” to the init function / to the class. When using “test(someObj)” you are passing the someObj variable to the __init__ function. The first argument will always be “self” for non-static functions
15th May 2024, 5:17 PM
Brentspine
Brentspine - avatar
+ 1
Sounds good. I was under impression of c++ and hence getting confused. I buy this argument and accept that derived class object can never be behaved as base class object
16th May 2024, 7:56 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
I saw the code. in the second last line you wrote "obj2 = test(obj1)" . the problem is that you gave 2 arguments to the class "test", one is self(by default) and another is obj1 ,but your class takes only one argument which is self by default. so don't give any argument to test. i hope your doubt is cleared
21st May 2024, 6:00 PM
Santanu
0
Then type cast in python for classes is applicable only to derived classes ? How to do type change in case of inheritance? Is it used to get derived from base or base from derived ? And how ?
15th May 2024, 3:44 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
I Don't want to reuse the variable. I need to behave A as some other type For example, b = 3.3 Int(b) will behave as int inspite 3.3 was decimal
15th May 2024, 4:03 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
In other words, can I get base class method invoked from y or derived class method invoked from x in attached code ? https://sololearn.com/compiler-playground/cLI3Si5x13Hh/?ref=app
15th May 2024, 4:34 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
A is animal and B is cat for example. And it is like cat(animal) for Inheritance hierarchy. Now, if I have objCat = Cat() how should I make objCat behave as animal? I don't want to create a new object using __init__ which takes cat object as argument for animal Class. I need objCat to behave as if it is animal class object which is parent of cat.
15th May 2024, 5:13 PM
Ketan Lalcheta
Ketan Lalcheta - avatar