Increment reference count of Python object - python-3.x

I have a static method that returns a new instance of it's class. The problem is that the instance is None in the code that called the method. I believe this is due to the fact the instance is created in a static method so it's reference count isn't incremented and it is released before the calling code can reference it. In C, I could call Py_INCREF() but I need to increment the reference count in Python code. What do I need to do?
class MyClass:
#staticmethod
def get(id: int) -> MyClass:
myobj = MyClass().configuredata()
return myobj

The issue here was not reference counting but rather that chaining of methods called a method that did not return self but instead returned None.

Related

Why can a function that has no return statement modify an attribute in a user-defined class instance but not a built in class instance?

I understand that if I pass a variable as an argument to a function it can only change the value of that argument within its own scope, not the value of the variable at the global scope.
However, this doesn't seem to apply to passing user-defined class instances to a function, where I am able to modify that object in the global scope without a return statement in the function.
For example, in the code below I would have expected the function bar() to only modify the values within the scope of the function, and not change the 'my_class_object' instance of foo in the global scope. However, when I run bar() I find that the value of my_class_object.the_value changes from 0 to 1.
class foo():
def __init__(self,num):
self.the_value = num
def bar(a,b):
a.the_value +=1
b +=1
my_class_object = foo(0)
int_class_object = 0
bar(my_class_object,int_class_object)
print(my_class_object.the_value)
print(int_class_object)

Is there any problem in calling a static method from type name in python?

Should I be aware of any problem that could arise from doing this?
Example:
class A(object):
def __init__(self, a):
self.a = a
#staticmethod
def add1(a):
return a+1
x = A(1)
y = type(x).add1(2)
My use case would be calling a static method that processes data that was generated by an object that we cannot use anymore.
Simple test for identity gives us:
x = A(1)
print(type(x) is A)
True
print(type(x).add is A.add)
True
So based on that there should not be any problem, but I am not 100% sure. Although I would probably go with accessing x.__class__ property, which is in my opinion more intuitive.
EDIT: From Python documentation regarding the type function:
With one argument, return the type of an object. The return value is a type object and generally the same object as returned by object.__class__.

How to properly use Iterators w/in Python? Python Beginner

I'm trying to clarify some confusion on the use of __iter__() and __next__() methods. Here's an example provided from the reading:
Create an iterator that returns numbers, starting with 1, and each sequence will increase by one (returning 1,2,3,4,5 etc.):
class MyNumbers:
def __iter__(self):
self.a = 1
return self
def __next__(self):
x = self.a
self.a += 1
return x
myclass = MyNumbers()
myiter = iter(myclass)
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
I'm trying to learn general patterns here, and am confused by myiter = iter(myclass). First the object myclass is created, and belongs to the class MyNumbers. This I understand.
Q: But what's going on with the way myiter is defined? It's a new object myiter set equal to an iter function I don't see defined, and including an entire class as a parameter? How does this work exactly? The reading further suggests these iteration methods to be analogous to __init__ but I don't see the relation. Hows the interpreter exactly go through this code?
Much thank for the time and help.
First of all, let's see the difference between iterable and iterator
In our day-to-day programming life, we use for-loop for often.
In below snippet you see that sampleList is iterable.
sampleList = [5,8,90,1,2]
for num in sampleList:
print num
How the above statement works: (steps involved in for-loop when it's executing)
for-loop gets/receives the iterator
if the iterator has the next item then sets the current index to next available item's index and execute code statement
if the iterator doesn't have the next item break the loop and come out of it
So now we understand that iterator can take action on the next item.
But how much information do we know about the iterator() and iterable() functions?
Iterator:
This contains a method iter() and next()
Here the iterator itself returns iter(). The primary key element to implement for num in sampleList
Iterable:
This contains a method iter(). Here this calls sampleList.iter() to get an iterator.
Now comes the next() definition. difference between the iterable and iterator object is next(). next() has the two action items on it whenever it called.
Update the iterator and point the index to the next item
return the current item.
First off, you're not "passing a classing as an argument". myclass is not a class, it's an instance of the class MyNumbers.
In this case, iter() calls the __iter__() method on thr object you pass, which you defined in the object's class. However, since your __iter__() implementation just returns the object itself, the call to iter() has no effect here.

Can someone tell me the function of this "self"

I use python to insert data into MySQL, in some tutorial, I must include "self" on my function,
Def haha(self, hihi):
Print(hihi)
I have no idea what does it mean..
This function takes instance of itself as an argument. It is done when declaring methods of a class because function needs to run the method from its own particular instance. Also self needed to access or update fields of particular instance of a class. For example:
class A:
b = 3
def f(self):
self.b = 5
So when you create object of type A and call method f from this object, only this object's b will become 5, but other objects of type A will have b equaling to 3. Also, when you call method, you do not pass instance of it as an argument, and can just start passing arguments starting after 'self'.

Calling closure on different object?

Suppose I have this class:
class MyClass {
int myInt
MyClass(myInt) {
this.myInt = myInt
}
def myMethod() {
print this.myInt
}
}
And somewhere I have:
def myClass1 = new MyClass(1)
def myMethodClosure = myClass1.&myMethod
def myClass2 = new MyClass(2)
Now if I call myMethodClosure() it will call myMethod() on myClass1 instance which will print 1. What I want is to call the same myMethodClosure but on a different instance, in this case on myClass2 so it can print 2. Is this possible?
I have tried using setDelegate(), but it does not work. I have also seen that there is field thisObject inside the closure class, but it does not have a setter, only a getter.
There were two methods added to Groovy to aid serialization of Closures, dehydrate and rehydrate. Basically, they strip (and reconstruct) a Closure's owner, thisObject and delegate. In this example, you could do:
myMethodClosure.rehydrate( myClass2, myClass2, myClass2 )()
To get the output 2, however I'd be wary about doing this as it is not what the method was intended for and there could be serious unforeseen consequences.
A better solution would probably be to write a factory method that gets a method reference for the given instance of MyClass. There may be other -- better -- solutions, but it depends on the situation you are in (that I suspect is not shown by the example in the question)
I believe closure "includes" the enclosing object to act upon. It is not possible override "this" inside a closure at runtime.

Resources