+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.

3/19/2020 10:33:30 PM

Karzan

32 Answers

New Answer

+8

✳AsterisK✳ Python supports arbitary function attributes. The example i have given is a working example.

+7

You mean something like this def test_one(): pass test_one.x = 3 def test_two(): y = test_one.x print(y) test_two()

+7

I just realised that people have already mentioned what I just posted.

+5

karzan Ok but it appears you are trying to do the same/similar. Anyways i don't know (at least as of now) what ecactly you are looking for.

+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...

+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

+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.

+5

Do you mean function attributes like this? https://code.sololearn.com/c2v1669aSe1g/?ref=app

+5

Kas tube Please do not spam someone's else thread with random/unrelated contents. Please follow the guidelines. Go through the link below. https://www.sololearn.com/Discuss/1316935/?ref=app

+4

am not sure that possible karzan the . is use for method which can only be found in a class

+4

karzan Hey we are not arguing here. We just want to help. So we are just providing the solution that is coming to the mind and answering what is asked by others or giving clarifications for what we wrote :)

+3

Are you sure you mean 'inherit'? Maybe a return value is all you need?

+3

karzan I did what i remembered at the time. See HonFu example. I did the same but outside of the function In python each function has a dictionary. The attribute will be added to it.

+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.

+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.

+2

Can you describe your plan precisely? What do you need it for?

+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.

+2

Not really inheritence but may be you are looking for "decorators"

+2

Maybe you mean decorators? https://www.sololearn.com/learn/Python/2463/?ref=app

+2

Haider Please do not post unnecessarily. Q/A section and someone else's thread is not a right place for Hi/Hello. Please follow the guidelines. Go through the link below. https://www.sololearn.com/Discuss/1316935/?ref=app