Can I inherit values from another function without a class in Python? How? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Can I inherit values from another function without a class in Python? How?

I want to inherit values from another function without creating a class to do that.

19th Mar 2020, 10:33 PM
Karzan
21 Answers
+ 7
I just realised that people have already mentioned what I just posted.
21st Mar 2020, 2:42 AM
Sonic
Sonic - avatar
+ 5
You can dynamically write attributes to a function, since they're also objects. def f(): f.x = 42 def g(): f() print(f.x) g() # output: 42 It's got nothing to do with inheritance, but does it do what you need? EDIT: Hm, Swim got the same idea, so prolly not...
19th Mar 2020, 11:16 PM
HonFu
HonFu - avatar
+ 5
but ~ swim ~ they are not called just like the method, in a class where you can then use method and its attributes, in that your example you defined text_one.x in the above function, not just using it when x is only defined in the function
19th Mar 2020, 11:21 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 5
karzan, we just wanted to help. If you prefer it, we can leave you alone. I seriously doubt that someone will be able to tell you anything about function inheritance, though. It's just not the right term.
19th Mar 2020, 11:26 PM
HonFu
HonFu - avatar
+ 5
Do you mean function attributes like this? https://code.sololearn.com/c2v1669aSe1g/?ref=app
21st Mar 2020, 2:39 AM
Sonic
Sonic - avatar
+ 4
am not sure that possible karzan the . is use for method which can only be found in a class
19th Mar 2020, 11:12 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 3
Are you sure you mean 'inherit'? Maybe a return value is all you need?
19th Mar 2020, 10:38 PM
HonFu
HonFu - avatar
+ 3
Guys I am not arguing but your guys trying to find others ways to solve the question but your answers are not right. I told you I have done it before so please if you don't know the answer wait until someone will give us the answer. If I figure it out in my old codes I will post the answer so we all learn.
19th Mar 2020, 11:23 PM
Karzan
+ 3
HonFu I appreciate the feedback and the answers I am just trying to find the correct answer. I am 100% sure I have done it before. Maybe it's not called inheritance. Maybe it's something else but it could be easily done in Python. Trust me.
19th Mar 2020, 11:29 PM
Karzan
+ 2
Can you describe your plan precisely? What do you need it for?
19th Mar 2020, 10:40 PM
HonFu
HonFu - avatar
+ 2
Are you maybe thinking of closures? Well, probably not, since that also needs return. Generators? They need yield, which is also a form of return. It would be best if you describe the program you have in mind, so that we get an idea of what you're trying to do.
19th Mar 2020, 10:51 PM
HonFu
HonFu - avatar
21st Mar 2020, 7:08 AM
Tibor Santa
Tibor Santa - avatar
+ 1
No I don't want to do return. I want to do it in another way. I remember I read it before but I can't remember.
19th Mar 2020, 10:39 PM
Karzan
+ 1
I need to inherit from another function without using a class as my question says. Return is not inheritance. So I don't want to use return.
19th Mar 2020, 10:41 PM
Karzan
+ 1
Ok let me provide an example and you help me with it. The example maybe wrong but I want to just show what I want to do: Def test_one(): x=3 Def test_two(): y=test_one.x Print(y) test_two()
19th Mar 2020, 10:57 PM
Karzan
+ 1
~ swim ~ no that is not what I want.
19th Mar 2020, 11:10 PM
Karzan
+ 1
✳AsterisK✳ it is possible I have done it before. I just don't remember. I am posting this hopefully someone can remind me.
19th Mar 2020, 11:13 PM
Karzan
+ 1
~ swim ~ In your example there is no need to the first function. It's useless because you ignored it and made it PASS. Also, you made test_one.x a variable.
19th Mar 2020, 11:17 PM
Karzan
+ 1
HonFu That is not what I want.
19th Mar 2020, 11:18 PM
Karzan
+ 1
21st Mar 2020, 7:14 AM
Noman S. Shiekh
Noman S. Shiekh - avatar