-2

Due to some circumstances I can only pass the argument to one function within a class.

Example:

class win1():
    def __init__(self):
        self.diction = dict()
        self.x = self.wanted_touse()
        #is it possible to make it here? If yes, it will be good to be like this
        # self.X = X or self.y = Y (is this even possible without passing an argument in it?)

    def fun(self, X,Y):
        self.x = X
        self.y = Y

    def wanted_touse(self):
        #I wanted to use X and Y here while at the same time I cannot pass the argument to this function because of some circumstances.
        #Here with some functions to make the dictionary for self.x example for x in enumerate(self.x)

        self.diction[something] = something

I would like to learn to see whether is it possible to use a variable within a function in win1 into want_touse function.

Joshua Schlichting
  • 3,110
  • 6
  • 28
  • 54
Beginner
  • 147
  • 8
  • Have you tried the approach above ? If you create X and Y in the __init__ method you should be able to access them via the self parameter in the other functions – pypypy Oct 25 '18 at 12:12
  • Yea, actually I wanted to make it in init but the problem is it wont pass it to init, it requires me to include argument in init in order to use the X or Y. – Beginner Oct 25 '18 at 12:13
  • Attributes set on `self` are still accessible to other methods on the same instance, yes. That's the point of classes; they bundle *state* (stored in attributes) and *functionality* (methods bound to the instance). – Martijn Pieters Oct 25 '18 at 12:13
  • @Beginner: You are free to set `self.X` and `self.Y` in `win1.fun()`, there is no requirement that attributes are set in `__init__` only. – Martijn Pieters Oct 25 '18 at 12:14
  • This question has example code, and a simple/clear explanation of what OP is trying to do. I don't understand why there are several down votes here. I think we are able to help @Beginner with this question. – Joshua Schlichting Oct 25 '18 at 12:45
  • thanks joshua. I think I think its because I didnt meet these professionals requirements thats why I get downvotes. Again, will try to make it as clear as possible next time. – Beginner Oct 26 '18 at 00:13

1 Answers1

1

Define your attribute in the __init__(), and then modify it in your fun() function, like this:

class win1():
    def __init__(self):
       self.x = None

    def fun(self, X,Y):
        self.x = X

    def wanted_touse(self):
        pass
        #do whatever you want with self.x here, so long as you've run your fun() function and changed self.x as you intended.

This example is just using a single variable. You can apply this to additional variables as needed.

Joshua Schlichting
  • 3,110
  • 6
  • 28
  • 54
  • 2
    There is no 'definition' of variables. The `__init__` part is optional. – Martijn Pieters Oct 25 '18 at 12:14
  • 1
    @martjin my understanding of best practice is that instance variables should always be initialized in __init__ even if it’s just to None. This improves readability greatly. – pypypy Oct 25 '18 at 12:16
  • @MartijnPieters I had not realized this. So, it's perfectly fine to refer to self.x for the first time within the fun() function, in this example? – Joshua Schlichting Oct 25 '18 at 12:18
  • @pypypy: While it can help, it is not a requirement, and is not always necessary. Attributes that are limited to a small subset of the functionality could lead to more clutter in `__init__` than it it is worth. – Martijn Pieters Oct 25 '18 at 12:25
  • @pypypy: and it's the terms *define* and *variable* here that I object to. Setting `self.x = None` is not an attribute definition. And I'd use the term *attribute*, not *variable*. – Martijn Pieters Oct 25 '18 at 12:27
  • @pypypy I agree that this would be best practice, as the gains in readability would have to outweigh any extra "bulk" the extra lines of code contribute to. By having the instance attributes in the __init__(), you can INSTANTLY see all the attributes that the instance is going to own. I didn't even realize it would be possible to use `self.attribute` on an attribute that wasn't defined in __init__(), as I've always done it this way. The more I think about it, the more it seems like a bad idea to declare instance attributes throughout the class. – Joshua Schlichting Oct 25 '18 at 12:27
  • actually, I tried something similar too but however in my def wanted_touse final line is creating a dictionary which will store in the init self.x = self.wanted_touse. I do this in wanted_touse function without return I put self.x[something] = something. But then I get TypeError: 'dict' object is not callable – Beginner Oct 25 '18 at 12:28
  • @JoshuaSchlichting: it is perfectly legal for `self.x` to be set in `win1.fun()` only. You could consider it an error to call `wanted_touse()` before `fun()`, or you could guard against `AttributeError` in `wanted_touse` or explicitly test for the `X` attribute being set, that's all fine. All that assigning to attributes does (in the non-slots case, the default) is set a key-value pair in the `__dict__` dictionary. See these like you would look at dictionary keys. – Martijn Pieters Oct 25 '18 at 12:29
  • @Beginner: the expressions you just posted don't call anything, there is no `object(...)` expression anywhere. Are you *sure* you used `self.x[something]` and not `self.x(something)` by accident? – Martijn Pieters Oct 25 '18 at 12:31
  • This discussion has been had by smarter people than me. https://stackoverflow.com/questions/20661448/python-should-all-member-variables-be-initialized-in-init in any case I will continue to write in the more readable style for my code. – pypypy Oct 25 '18 at 12:31
  • @MartijnPieters Thanks for the new-to-me information. Also, you're right, we should be calling these attributes, not simple variables. I'll keep this in mind, however, I do think it is best to have them declared in the __init__(). Proper documentation/comments might suffice as well. I also understand that this one way of thinking might not apply to all scenarios. – Joshua Schlichting Oct 25 '18 at 12:31
  • 2
    @pypypy: I'm not discussing if it is a better style idea or not. All I'm doing is pointing out that there is no *technical* need to do so. – Martijn Pieters Oct 25 '18 at 12:32
  • martijn I am sorry, I just updated. Was editing just now – Beginner Oct 25 '18 at 12:35