Python Forum
How to Create Very Very Special Class with too many magic methods ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Create Very Very Special Class with too many magic methods ?
#1
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
Reply
#2
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
Reply
#3
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'
Reply
#4
>>> 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?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
@wavic, you are right. i wanted only good code view but i didn't know it was so difficult. thanks
Reply
#6
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class test : good way to split methods into several files paul18fr 4 465 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  all of attributes and methods related to a special type akbarza 4 471 Jan-19-2024, 01:11 PM
Last Post: perfringo
  Structuring a large class: privite vs public methods 6hearts 3 1,043 May-05-2023, 10:06 AM
Last Post: Gribouillis
  magic related field in Django model sonh 1 1,230 Apr-24-2022, 12:37 PM
Last Post: sonh
  Cannot convert the series to <class 'int'> when trying to create new dataframe column Mark17 3 8,471 Jan-20-2022, 05:15 PM
Last Post: deanhystad
  Need a little help with numpy array magic. pmf71 0 1,140 Dec-01-2021, 02:51 AM
Last Post: pmf71
  access share attributed among several class methods drSlump 0 1,056 Nov-18-2021, 03:02 PM
Last Post: drSlump
  a function common to methods of a class Skaperen 7 2,565 Oct-04-2021, 07:07 PM
Last Post: Skaperen
  Listing All Methods Of Associated With A Class JoeDainton123 3 2,330 May-10-2021, 01:46 AM
Last Post: deanhystad
  Create Generator in the Class quest 1 2,118 Apr-15-2021, 03:30 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020