Python Forum

Full Version: How to Create Very Very Special Class with too many magic methods ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i wanted a PointerVariable Class. but many problems came out. i want to understand what i want with following code. 

class PointerVariable()
    def __init__(self, val = None):
        self.val = val
        
    def for_use_of_variable_without_calling(self): # this function runs only when variable is used without calling
        return  self.val                                      # __repr__ function only returns string so i don't want use it
    
    def new_value(self, val): # this function for use of "myPointerVariable = 234" expressions
        self.val = val          # example: "myPointerVariable = 234" expression is run the this function as self.new_value(234)
        
    
example = PointerVariable("foo")
example = "bar" # this expression is run the new_value functiıon as self.new_value("bar")
example # this expression is run for_use_of_variable_Without_calling_event function as with no args
is there a any magic method for do this instead of my exapmle functions
Although many call double underscore methods 'Magic methods', I prefer the term dunder.
I think the jury's still out on that one.

At any rate, there are some good blogs out there on this subject that you might want to take a look at.
One of the best (my opinion) is here: http://minhhh.github.io/posts/a-guide-to...ic-methods

I don't think there's anything for your 'for_use_of_variable_without_calling' method, but I think that
with a little thought you could do it. As a matter of fact, the thinter label textvariable does pretty much
that. My suggestion would be to look at how that's implemented. (It's probably in C and part of TK, but
perhaps not). You can dig into the code here: (on windows): c:\python36\lib\tkinter\__init__.py

Replace c:\python36 with your python path

The textvariable code is here: http://wiki.tcl.tk/1917
I can not understand what you mean.
This make no sense example = "bar"
This just overwrite class object example.
There are no function in your example,it's called method when belong to a class.
If a function is placed in class it has @staticmethod decorator.

To run the class just to make it clear what it dos:
>>> example = PointerVariable("foo")
>>> example.for_use_of_variable_without_calling()
'foo'
>>> example.val
'foo'

# Change val to bar
>>> example.new_value('bar')
>>> example.for_use_of_variable_without_calling()
'bar'
>>> example.val
'bar'
>>> class example():
...     pass

>>> foo = example()

>>> foo.x = 10 # new variable

>>> foo.y = 20 # new variable

>>> foo.sum = lambda a, b: a + b 

>>> foo.sum(10, 20)
30

>>> foo.sum(foo.x, foo.y)
30

>>> foo.x = 20

>>> foo.sum(foo.x, foo.y) 
40

>>> class example2():
...     x = 5
...     y = 7

>>> bar = example2()

>>> bar.x
5

>>> bar.x = 12

>>> bar.x
12
It could be simple as this one. Why to define a method just to change a variable?
@wavic, you are right. i wanted only good code view but i didn't know it was so difficult. thanks
I am not saying that you have to do it that way. I am saying that your code snippet is overcomplicated for what is doing.