How to cast in python : 2 | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 3

How to cast in python : 2

Hi I have a question on casting derived object to base class without loosing data. How to do this in python? I came from c++ background and this is feasible in C++ so checking about same in python . Is it possible or not? If yes, how. Refer code below. It has sample code and question also in comment section of code. https://sololearn.com/compiler-playground/cb1G9SPjxs85/?ref=app

17th May 2024, 5:24 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
11 Antworten
+ 2
if you want to create a child class you need to type class Class_Name(Parent_Class_Name): The new class will have the same functions
17th May 2024, 5:54 PM
­čç║­č玭čç║­č玭čç║­čçŽCoding Nerd­čç║­č玭čç║­č玭čç║­čçŽ
­čç║­č玭čç║­č玭čç║­čçŽCoding Nerd­čç║­č玭čç║­č玭čç║­čçŽ - avatar
+ 2
The null value of the _a attribute for the display method of the inherited class is initially in the namespace of the base class. Whereas during its update this space is changed to the local one of the instance itself. Therefore, you will not be able to see it by calling the display method on the base class. In this case, I suggest using the data descriptor, for which the search for instances scans the chain of namespaces, giving them the highest priority. https://sololearn.com/compiler-playground/cCvlV5tKdow9/?ref=app
24th May 2024, 1:17 AM
Vitaly Sokol
Vitaly Sokol - avatar
+ 1
Modified title.
17th May 2024, 5:28 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Could you please elaborate on below­čç║­č玭čç║­č玭čç║­čçŽCoding Nerd­čç║­č玭čç║­č玭čç║­čçŽ ? "if you want to create a child class you need to type class Class_Name(Parent_Class_Name): The new class will have the same functions" Does this mean inheritance is not needed and still I just get all methods of one class in other Class? If not, then what you wanted convey ? If yes, what's the way to do so.?
18th May 2024, 9:05 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Beno├«t Tagbino Je ne sais pas pourquoi vous demandez cela ici. Si vous avez une question sur la programmation, vous devriez la poser dans un article s├ępar├ę. SÔÇÖil vous pla├«t, nÔÇÖessayez pas de lÔÇÖajouter ├á celui de quelquÔÇÖun dÔÇÖautre. Cette r├ęponse a ├ęt├ę traduite par machine de lÔÇÖanglais pour votre commodit├ę.
19th May 2024, 4:15 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
­čś╣DonÔÇÖt title your question on how clear and well written the question is, lol. That being said it does look pretty clear this time, but with virtually no C++ knowledge IÔÇÖm not sure what to tell you.
17th May 2024, 5:27 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
IÔÇÖm not sure why youÔÇÖre trying to force one programming language into anotherÔÇÖs working mechanic. What are you actually trying to do in the program? ItÔÇÖs like you want to learn to fly whilst following automobile traffic laws.
17th May 2024, 5:35 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
And you cant cast in Python if im not mistaken
18th May 2024, 7:58 AM
­čç║­č玭čç║­č玭čç║­čçŽCoding Nerd­čç║­č玭čç║­č玭čç║­čçŽ
­čç║­č玭čç║­č玭čç║­čçŽCoding Nerd­čç║­č玭čç║­č玭čç║­čçŽ - avatar
0
­čç║­č玭čç║­č玭čç║­čçŽCoding Nerd­čç║­č玭čç║­č玭čç║­čçŽ python does allow casting for basic data type like below: b = 1.3 print(int(b)) prints 1 and casting happens. Right? Or this is also an object creation only?
18th May 2024, 9:07 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Tu parle fran├žais?
19th May 2024, 11:44 AM
Benoît Tagbino
0
Wilbur Jaywright. Yes
8th Jun 2024, 8:54 AM
­čç║­č玭čç║­č玭čç║­čçŽCoding Nerd­čç║­č玭čç║­č玭čç║­čçŽ
­čç║­č玭čç║­č玭čç║­čçŽCoding Nerd­čç║­č玭čç║­č玭čç║­čçŽ - avatar