0

I want use a class master to control status flag to every class which I want to maintain.

But I only find one way to dynamic add function and var like below.(staticmethod and classmethod don't work)

In my case,simple variable like integer ,float,bool can't call by object sharing

I do some research from link below, there are two ways to overcome this problem => one element list or self-defined class

Passing an integer by reference in Python

I wish can find a way perfectly inheritance just like class A(Master):

or instance_a = A(),some_fun(instance_a,Master)

help me to inheritance when creat instance

Do any one know how to achieve that

class A():
    pass

class Master():
    g_list = []
    g_var = 0    
    def test_method(self,val):
        print('test',val)

m_attr_list = [i for i in Master.__dict__.keys() if not i.startswith('__')]

for name in m_attr_list:    
    setattr(A, name, getattr( Master, name))
黃瀚嶙
  • 167
  • 1
  • 10
  • 1
    "In my case,simple variable like integer ,float,bool can't pass by reference" *nothing* is ever pass by reference in Python, regardless of the type – juanpa.arrivillaga Nov 07 '19 at 03:18
  • @juanpa.arrivillaga I would actually say that everything is passed by reference – gmds Nov 07 '19 at 03:19
  • @gmds and you would be wrong. To be pass by reference, changes to the parameter in the function must be *seen in the caller*. That is never possible in Python. A quick rule of thumb: if you cannot write a `swap` function, such that `a = 1; b = 2; swap(a, b); print(a, b)` prints `2, 1` then you can't pass by reference. Python uses an evaluation strategy named "call by object sharing", or sometimes "call by assignment" – juanpa.arrivillaga Nov 07 '19 at 03:21
  • @gmds is right,even a = 5, b = a is a passing id(ref) to b – 黃瀚嶙 Nov 07 '19 at 03:23
  • 1
    @黃瀚嶙 no, it isn't. That isn't what "call by reference means". These words have precise definitions. And Python **does not support call by reference**. And the semantics of *assignment* don't have anything to do with "passing by reference", which refers to an evaluation strategy for function arguments. – juanpa.arrivillaga Nov 07 '19 at 03:24
  • @juanpa.arrivillaga Strictly speaking, yes, I would be wrong. Nevertheless, do you really see that type of "pass-by-reference" any more? – gmds Nov 07 '19 at 03:25
  • @juanpa.arrivillaga you mean even ,a = [ 1,2,3], b=a is not passing by reference? Because I write b = 5 and a will not change? – 黃瀚嶙 Nov 07 '19 at 03:26
  • @gmds no, which is why I don't call it "pass-by-reference". And just because I personally don't see it doesn't mean that other people don't, e.g. C++ is still prominently used and it supports call by reference semantics. – juanpa.arrivillaga Nov 07 '19 at 03:26
  • @黃瀚嶙 yes, that has *nothing to do with call by reference*, which has to do with the way arguments to functions work. That is simply a category error, one that likely arises because people incorrectly use "call by reference" to mean things that it doesn't mean. Here is a wikipedia article on the various evaluation strategies, and it talks about Python's strategy: https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing – juanpa.arrivillaga Nov 07 '19 at 03:27
  • @juanpa.arrivillaga but that is link name...how do I change it? – 黃瀚嶙 Nov 07 '19 at 03:29
  • Every class should use g_list.append(self) so I can use Master to control all class of their instance which inheriting from Master. – 黃瀚嶙 Nov 07 '19 at 09:10
  • I just want to use class A inherit class B but inherit when I create instance. normally,we use class A(B). I want to find some function that instance = fun(A,B) – 黃瀚嶙 Nov 11 '19 at 02:09

0 Answers0