+ 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.
21 Réponses
+ 7
I just realised that people have already mentioned what I just posted.
+ 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
+ 4
am not sure that possible karzan the . is use for method which can only be found in a class
+ 3
Are you sure you mean 'inherit'?
Maybe a return value is all you need?
+ 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.
+ 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
Maybe you mean decorators?
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2463/?ref=app
+ 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.
+ 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.
+ 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()
+ 1
~ swim ~ no that is not what I want.
+ 1
✳AsterisK✳ it is possible I have done it before. I just don't remember. I am posting this hopefully someone can remind me.
+ 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.
+ 1
HonFu That is not what I want.
+ 1
https://code.sololearn.com/cXQcWtlK3t8N/?ref=app
maybe you are looking for this