How to create an inline function that modifies a variable in Python? - python

Suppose I have a class that looks like this:
class Foo:
def __init__(self, method):
self.method = method
def do_action(self):
self.method()
and I want to instantiate it as follows:
some_var = False
def bar():
# Modifies existing variable's value
global some_var
some_var = True
foo = Foo(bar)
how do I do that without having to define the bar() method?
I've tried the following and it doesn't work.
foo = Foo(lambda: (some_var := True))
When I do this the IDE tells me there's an identifier expected. Thanks in advance!
EDIT:
Thank you to those who answered, however I didn't really find exactly what I needed. Not sure if it's the best practice, but I ended up using python's exec and it works as intended:
foo = Foo(lambda: exec("some_var = True"))

If the idea is to have bar be able to modify an instance attribute, have bar take the self parameter so do_action can tell it which instance it's operating on. You can't do a variable assignment inside a lambda, so use __setattr__:
class Foo:
def __init__(self, method):
self.method = method
self.some_var = False
def do_action(self):
self.method(self)
foo = Foo(lambda self: self.__setattr__("some_var", True))
foo.do_action()
print(foo.some_var) # True

Related

Given a Python method, how to get its class?

Suppose I have this class:
class Foo():
def foo_method(self):
pass
Now suppose I have an object foo = Foo().
I can pass foo.foo_method around as argument to a function.
foo.foo_method.__qualname__ returns the string representing the method's "full name":
"Foo.foo_method".
What if I want to get Foo, the class itself, from foo.foo_method?
The solution I came up with is:
def method_class(method):
return eval(method.__qualname__.split(".")[0])
Is there a less "dirty" way of achieving this?
The instance that a bound method is bound to, is stored as the __self__ attribute. Thus:
class Foo:
def foo_method(self):
pass
foo = Foo()
assert foo.foo_method.__self__.__class__ is Foo
The following might do what you want:
##########################################################
class Klassy:
def methy(arg):
pass
insty = Klassy()
funky = insty.methy
##########################################################
insty_jr = funky.__self__
Klassy_jr = type(insty_jr)
print(Klassy_jr)

How to overwrite self after reading yaml? [duplicate]

I would like to replace an object instance by another instance inside a method like this:
class A:
def method1(self):
self = func(self)
The object is retrieved from a database.
It is unlikely that replacing the 'self' variable will accomplish whatever you're trying to do, that couldn't just be accomplished by storing the result of func(self) in a different variable. 'self' is effectively a local variable only defined for the duration of the method call, used to pass in the instance of the class which is being operated upon. Replacing self will not actually replace references to the original instance of the class held by other objects, nor will it create a lasting reference to the new instance which was assigned to it.
As far as I understand, If you are trying to replace the current object with another object of same type (assuming func won't change the object type) from an member function. I think this will achieve that:
class A:
def method1(self):
newObj = func(self)
self.__dict__.update(newObj.__dict__)
It is not a direct answer to the question, but in the posts below there's a solution for what amirouche tried to do:
Python object conversion
Can I dynamically convert an instance of one class to another?
And here's working code sample (Python 3.2.5).
class Men:
def __init__(self, name):
self.name = name
def who_are_you(self):
print("I'm a men! My name is " + self.name)
def cast_to(self, sex, name):
self.__class__ = sex
self.name = name
def method_unique_to_men(self):
print('I made The Matrix')
class Women:
def __init__(self, name):
self.name = name
def who_are_you(self):
print("I'm a women! My name is " + self.name)
def cast_to(self, sex, name):
self.__class__ = sex
self.name = name
def method_unique_to_women(self):
print('I made Cloud Atlas')
men = Men('Larry')
men.who_are_you()
#>>> I'm a men! My name is Larry
men.method_unique_to_men()
#>>> I made The Matrix
men.cast_to(Women, 'Lana')
men.who_are_you()
#>>> I'm a women! My name is Lana
men.method_unique_to_women()
#>>> I made Cloud Atlas
Note the self.__class__ and not self.__class__.__name__. I.e. this technique not only replaces class name, but actually converts an instance of a class (at least both of them have same id()). Also, 1) I don't know whether it is "safe to replace a self object by another object of the same type in [an object own] method"; 2) it works with different types of objects, not only with ones that are of the same type; 3) it works not exactly like amirouche wanted: you can't init class like Class(args), only Class() (I'm not a pro and can't answer why it's like this).
Yes, all that will happen is that you won't be able to reference the current instance of your class A (unless you set another variable to self before you change it.) I wouldn't recommend it though, it makes for less readable code.
Note that you're only changing a variable, just like any other. Doing self = 123 is the same as doing abc = 123. self is only a reference to the current instance within the method. You can't change your instance by setting self.
What func(self) should do is to change the variables of your instance:
def func(obj):
obj.var_a = 123
obj.var_b = 'abc'
Then do this:
class A:
def method1(self):
func(self) # No need to assign self here
In many cases, a good way to achieve what you want is to call __init__ again. For example:
class MyList(list):
def trim(self,n):
self.__init__(self[:-n])
x = MyList([1,2,3,4])
x.trim(2)
assert type(x) == MyList
assert x == [1,2]
Note that this comes with a few assumptions such as the all that you want to change about the object being set in __init__. Also beware that this could cause problems with inheriting classes that redefine __init__ in an incompatible manner.
Yes, there is nothing wrong with this. Haters gonna hate. (Looking at you Pycharm with your in most cases imaginable, there's no point in such reassignment and it indicates an error).
A situation where you could do this is:
some_method(self, ...):
...
if(some_condition):
self = self.some_other_method()
...
return ...
Sure, you could start the method body by reassigning self to some other variable, but if you wouldn't normally do that with other parametres, why do it with self?
One can use the self assignment in a method, to change the class of instance to a derived class.
Of course one could assign it to a new object, but then the use of the new object ripples through the rest of code in the method. Reassiging it to self, leaves the rest of the method untouched.
class aclass:
def methodA(self):
...
if condition:
self = replace_by_derived(self)
# self is now referencing to an instance of a derived class
# with probably the same values for its data attributes
# all code here remains untouched
...
self.methodB() # calls the methodB of derivedclass is condition is True
...
def methodB(self):
# methodB of class aclass
...
class derivedclass(aclass):
def methodB(self):
#methodB of class derivedclass
...
But apart from such a special use case, I don't see any advantages to replace self.
You can make the instance a singleton element of the class
and mark the methods with #classmethod.
from enum import IntEnum
from collections import namedtuple
class kind(IntEnum):
circle = 1
square = 2
def attr(y): return [getattr(y, x) for x in 'k l b u r'.split()]
class Shape(namedtuple('Shape', 'k,l,b,u,r')):
self = None
#classmethod
def __repr__(cls):
return "<Shape({},{},{},{},{}) object at {}>".format(
*(attr(cls.self)+[id(cls.self)]))
#classmethod
def transform(cls, func):
cls.self = cls.self._replace(**func(cls.self))
Shape.self = Shape(k=1, l=2, b=3, u=4, r=5)
s = Shape.self
def nextkind(self):
return {'k': self.k+1}
print(repr(s)) # <Shape(1,2,3,4,5) object at 139766656561792>
s.transform(nextkind)
print(repr(s)) # <Shape(2,2,3,4,5) object at 139766656561888>

Python: looking for a short-hand way to setup setters/getters for lots of variables

I have one class (Bar) embedded inside another class (Foo).
class Foo():
class Bar():
def __init__(self):
self.a = 1
self.b = 2
...
self.z = 26
def __init__(self):
self.bar = Bar()
To access the attributes of class Bar, the user would need to the following:
>>> f = Foo()
>>> f.bar.a
1
How can I setup a short dot notation so that users can use BOTH:
>>> f.bar.a
1
and
>>> f.a
1
In my example, I'm trying to demonstrate that Bar class has a lot of variables. So I don't want to write a getter/setter for each one manually. So I was thinking to use the property() in a for loop like this:
def __init__(self):
self.bar = Bar()
# Allow shorter dot notation
for parm in self.bar.__dict__:
setattr(self, i, getattr(bar, i))
self.i = property(...)
But I'm unsure how to use property in this context without manually writing several setter functions.
Any suggestions on how to allow access to both shorter and longer notations?
That's what the __getattr__hook is ideally suited for:
class Foo:
# ...
def __getattr__(self, name):
return getattr(self.bar, name)
__getattr__ is only called for attributes that are missing; so only attributes that are not already present on instances of Foo() are passed to Foo().__getattr__(). The getattr() function then lets you use the same attribute name on self.bar; if the attribute doesn't exist there either, an AttributeError is thrown, as would be expected.

Python - method of a class with an optional argument and default value a class member

I have something like this (I know this code doesn't work, but it's the closer to what I want to achieve):
class A:
def __init__(self):
self.a = 'a'
def method(self, a=self.a):
print a
myClass = A()
myClass.method('b') # print b
myClass.method() # print a
What I've done so far, but I do not like it, is:
class A:
def __init__(self):
self.a = 'a'
def method(self, a=None):
if a is None:
a = self.a
print a
myClass = A()
myClass.method('b') # print b
myClass.method() # print a
Default arguments are evaluated at definition time. By the time the class and method are defined self.a is not.
Your working code example is actually the only clean way of achieving this behavior.
The default is evaluated at method definition time, i.e. when the interpreter executes the class body, which usually happens only once. Assigning a dynamic value as default can only happen within the method body, and the approach you use is perfectly fine.

Does there have to be a body to the init method in python?

Am I allowed to not put any parameters besides self into the init method? Can I also have a body that does not define any other variables besides self?
class foo(object):
def __init__(self):
self = self
Or do I have to put a parameter and a body like so:
class bar(object):
def __init__(self, x):
self.x = x
If you have nothing to initialize, than you don't need to define an __init__ method at all. Just don't define it.
If you are not using the __init__, then there is no reason to define it at all. However, if this is theoretical, then I have a question for you: Why don't you try it first?
For the sake of completion, here is my trial:
>>> class foo(object):
... def __init__(self):
... self = self
...
>>> foo()
<__main__.foo object at 0x1004afa50>
>>> x = foo()
>>>
So an answer: yes you can do this.
Just a point, instead of calling self = self, just put pass; it was built to be a placeholder in situations like this.

Categories