Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, you don't need to specify types in Python. So, if you want <code>method</code> to take a <code>First</code> instance, just do this:</p> <pre><code>class Second(): def __init__(self, c, d): pass def method(self, first): pass my_first = First(0, 1) my_second = Second(2, 3) my_second.method(my_first) </code></pre> <p>I believe that answers your real question, which is:</p> <blockquote> <p>If I have several instances of the class first and need to pass any of them not specifying which one to method of class Second…</p> </blockquote> <p>If you want to ensure that the parameter actually is a First, you can always add an <code>assert isinstance(first, First)</code> or <code>if not isinstance(first, First): raise TypeError</code> or whatever, but usually you don't want to do that in Python. The whole point of "duck typing" is that you write a function that takes "anything that acts like a <code>First</code> instance" rather than a function that takes "a <code>First</code> instance".</p> <hr> <p>You then say:</p> <blockquote> <p>Now I need to mutate variables from the First class inside the method of a second class:</p> </blockquote> <p>So… just do it. Your example doesn't have any attributes in the <code>First</code> class, so let's add some:</p> <pre><code>class First(): def __init__(self, a, b): self.total = a + b </code></pre> <p>And now, let's use them in <code>Second.method</code>:</p> <pre><code>class Second(): def __init__(self, c, d): self.total = c + d def method(self, first): first.total += self.total </code></pre> <p>So:</p> <pre><code>&gt;&gt;&gt; my_first = First(0, 1) &gt;&gt;&gt; my_first.total 1 &gt;&gt;&gt; my_second = Second(2, 3) &gt;&gt;&gt; my_second.total 5 &gt;&gt;&gt; my_first.total += 2 &gt;&gt;&gt; my_first.total 3 &gt;&gt;&gt; my_second.method(my_first) &gt;&gt;&gt; my_first.total 8 </code></pre> <hr> <p>Or, if you meant that you wanted to mutate the <em>class</em> attributes in class <code>First</code>… you don't even need a <code>First</code> instance for that:</p> <pre><code>First.my_class_attribute = 1 </code></pre> <hr> <p>If you really <em>do</em> need to pass a class itself… well, a class is a regular value like anything else:</p> <pre><code>class Second(): def __init__(self, c, d): pass def method(self, cls): pass my_second = Second(1, 2) my_second.method(First) </code></pre> <p>And you can access the class attributes of <code>cls</code> from within <code>method</code>, just as easily as you can access instance attributes when an instance is passed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload