-2

I made the method name is slide. and it must return the reference of it. Then, What should I do?.

First, I made this function like:

class N:
  def slide(self, i):
    #do something
    return self

This slide method must return "N()" but "<~~ obejct at x0000>"

Minwoo Yu
  • 360
  • 2
  • 13
  • 2
    Please update your question with your calling code. What do you want to do with the return value from `slide()`? – quamrana Apr 02 '22 at 14:43
  • Your code isn't very clear, but I think you're looking for `__repr__`. See https://stackoverflow.com/q/1436703/354577 – ChrisGPT was on strike Apr 02 '22 at 14:49
  • 1
    I see you have updated the question, but its still not clear. Did you mean to write: `return "N()"`, or are you after the `__repr__()` method? – quamrana Apr 02 '22 at 15:52

1 Answers1

-2

Definitely depends on your calling code. But this, for example, will work as expected to return a reference to an instantiated object N:

class N:
  i = 1

  def slide(self, i):
    self.i = i;

    return self

n = N()
print(n.i) # 1

r = n.slide(2)
print(r.i) # 2
gerowam
  • 383
  • 1
  • 11
  • How does this meaningfully differ from what OP has in their question? Both define a class `N` with a method `slide()` that returns `self`. Why do you think OP is asking how to access `i`? – ChrisGPT was on strike Apr 02 '22 at 15:03
  • But in my python interpreter, it doesn't accept a class without an init function at all – EasyWay Coder Apr 02 '22 at 15:15
  • `i` is just to demonstrate `slide()` works as expected. It doesn't different significantly from OP; but demonstrates a working way to call `slide()` and receive a reference in return. – gerowam Apr 02 '22 at 15:29
  • I don't believe OP's question is in any way addressed here, but it is very unclear. OP needs to clarify their problem. I still think they just want to define a `__repr__()` for their class. – ChrisGPT was on strike Apr 02 '22 at 18:43