Is is possible to access the arguments which were passed to __init__, without explicitly having to store them?
e.g.
class thing(object):
def __init__(self, name, data):
pass # do something useful here
t = thing('test', [1,2,3,])
print t.__args__ # doesn't exist
>> ('test', [1,2,3])
The use-case for this is creating a super-class which can automatically store the arguments used to create an instance of a class derived from it, without having to pass all the arguments explicitly to the super's __init__. Maybe there's an easier way to do it!
No, you have to store them. Otherwise they are gone after __init__() returns, as all local variables.
If you don't want to pass all arguments on explicitly, you can use **kwargs:
class Base(object):
def __init__(self, name, data):
# store name and data
class Derived(Base):
def __init__(self, **kwargs):
Base.__init__(self, **kwargs)
Derived(name="Peter", data=42)
This is not entirely recommended, but here is a wrapper that automatically stores parameter variables:
from functools import wraps
def init_wrapper(f):
#wraps(f)
def wrapper(self, *args, **kwargs):
func_parameters = f.func_code.co_varnames[1:f.func_code.co_argcount]
#deal with default args
diff = len(func_parameters) - len(args)
if diff > 0:
args += f.func_defaults[-diff:]
#set instance variables
for pos, arg in enumerate(func_parameters):
print pos, arg
setattr(self, arg, args[pos])
f(self, *args, **kwargs) #not necessary to use return on __init__()
return wrapper
Usage:
class A(object):
#init_wrapper
def __init__(self, a, b, c):
print a + b + c
Example:
>>> a = A(1, 2, 3)
6
>>> a.a
1
>>> a.b
2
>>> a.c
3
In a word: No.
What you could do is:
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
If you find yourself needing to do this a lot, you could also use a decorator to abstract the task.
I think that you are looking for arbitrary argument lists and keyword arguments combined with super.__init__.
Give "Python's Super is nifty, but you can't use it" a read before you start down this path though.
Related
I am trying to design a class structure that allows the user to define their own class that overloads predefined methods in other classes. In this case the user would create the C class to overload the "function" method in D. The user created C class has common logic for other user created classes A and B so they inherit from C to overload "function" but also inherit from D to use D's other methods. The issue I am having is how to pass "value" from A and B to D and ignore passing it to C. What I currently have written will produce an error as C does not have "value" as an argument.
I know that I can add "value" (or *args) to C's init method and the super call but I don't want to have to know what inputs other classes need in order to add new classes to A and B. Also, if I swap the order of C and D I won't get an error but then I don't use C's overloaded "function". Is there an obvious way around this?
class D(SomethingElse):
def __init__(self, value, **kwargs):
super(D, self).__init__(**kwargs)
self.value = value
def function(self):
return self.value
def other_method(self):
pass
class C(object):
def __init__(self):
super(C, self).__init__()
def function(self):
return self.value*2
class B(C, D):
def __init__(self, value, **kwargs):
super(B, self).__init__(value, **kwargs)
class A(C, D):
def __init__(self, value, **kwargs):
super(A, self).__init__(value, **kwargs)
a = A(3)
print(a.function())
>>> 6
Essentially, there are two things you need to do to make your __init__ methods play nice with multiple inheritance in Python:
Always take a **kwargs parameter, and always call super().__init__(**kwargs), even if you think you are the base class. Just because your superclass is object doesn't mean you are last (before object) in the method resolution order.
Don't pass your parent class's __init__ arguments explicitly; only pass them via **kwargs. Your parent class isn't necessarily the next one after you in the method resolution order, so positional arguments might be passed to the wrong other __init__ method.
This is called "co-operative subclassing". Let's try with your example code:
class D:
def __init__(self, value, **kwargs):
self.value = value
super().__init__(**kwargs)
def function(self):
return self.value
class C:
# add **kwargs parameter
def __init__(self, **kwargs):
# pass kwargs to super().__init__
super().__init__(**kwargs)
def function(self):
return self.value * 2
class B(C, D):
# don't take parent class's value arg explicitly
def __init__(self, **kwargs):
# pass value arg via kwargs
super().__init__(**kwargs)
class A(C, D):
# don't take parent class's value arg explicitly
def __init__(self, **kwargs):
# pass value arg via kwargs
super().__init__(**kwargs)
Demo:
>>> a = A(value=3)
>>> a.value
3
>>> a.function()
6
Note that value must be passed to the A constructor as a keyword argument, not as a positional argument. It's also recommended to set self.value = value before calling super().__init__.
I've also simplified class C(object): to class C:, and super(C, self) to just super() since these are equivalent in Python 3.
So I'm trying to understand the point of A AND B. I'm guessing that maybe you want to mix in the superclass behavior and sometimes have local behavior. So suppose A is just mixing together behaviors, and B has some local behavior and state.
If you don't need your own state, you probably don't need an __init__. So for A and C just omit __init__.
class SomethingElse(object):
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
class D(SomethingElse):
def __init__(self, value, *args, **kwargs):
super(D, self).__init__(*args, **kwargs)
self.value = value
def function(self):
return self.value
def other_method(self):
return self.__dict__
class C(object):
#def __init__(self):
# super(C, self).__init__()
def function(self):
return self.value*2
class B(C, D):
def __init__(self, value, bstate, *args, **kwargs):
super(B, self).__init__(value, *args, **kwargs)
self.bstate = bstate
def __repr__(self):
return (self.__class__.__name__ + ' ' +
self.bstate + ' ' + str(self.other_method()))
class A(C, D):
pass
a = A(3)
b = B(21, 'extra')
a.function()
6
b.function()
42
repr(a)
'<xx.A object at 0x107cf5e10>'
repr(b)
"B extra {'args': (), 'bstate': 'extra', 'value': 21, 'kwargs': {}}"
I've kept python2 syntax assuming you might still be using it, but as another answer points out, python3 simplifies super() syntax, and you really should be using python3 now.
If you swap C and D you are changing the python method resolution order, and that will indeed change the method to which a call to A.function resolves.
I need a magic variable in my class and started writing it
class myclass:
def __init__(self, name, *args):
self.name = name
????
def myFunc()
for i in args:
print(i)
I just could not find a proper explanation of how to write a class with magic variables in the constructor and use it later. Do I have to create a self. member out of it (and if so how) or can I neglect it and just use args as in myFunc ?
*args are not called magic variables, but arbitrary argument lists, or variadic arguments, and they are used to send arbitrary number of arguments to a function, and they are wrapped in a tuple like the example below
In [9]: def f(a,*args):
...: print(a)
...: print(args)
...:
In [10]: f(1,2,3,4)
1
(2, 3, 4)
So in order to access these variables, you would do what you do for any class instance variable, assign it via self.args = args and access them via self.args
Also note that we use camel-case for class names, so the class name changes to MyClass and snake-case for functions, so the function name changes to my_func
class MyClass:
def __init__(self, name, *args):
self.name = name
#Assigning variadic arguments using self
self.args = args
def my_func(self):
#Accessing variadic arguments using self
for i in self.args:
print(i)
obj = MyClass('Joe',1,2,3)
obj.my_func()
The output will be
1
2
3
If you assign the args to self.args, you can access them from other methods. However this method must accept the instance of the class as its first argument (see below how myFunc takes self now).
class myclass:
def __init__(self, name, *args):
self.name = name
self.args = args
def myFunc(self):
for i in self.args:
print(i)
I am trying to learn more about Python - I am using 3.6.3 currently.
What is the best practice for argument validation/parsing when using kwargs?
Or, what is the best practice for argument validation/parsing without kwargs?
class foo:
def __init__(self, **kwargs):
if 'a' in kwargs:
self.a = kwargs['a']
else:
self.a = 0
class bar(foo):
def __init__(self, **kwargs):
super().__init__()
if 'x' in kwargs:
self.x = kwargs['x']
else:
self.x = 23
# b and y are undefined, but my classes use kwargs - what is the best practice for validating / parsing dynamic arguments?
test = bar(b=1,y=24)
You can pass a default value to get() for keys that are not in the kwargs dictionary:
def __init__(self, **kwargs):
self.a = kwargs.get("a", 0)
self.x = kwargs.get("x", 23)
# etc.
Alternatively if you want any value in kwargs to be set as an instance variable of your class, you could do:
def __init__(self, **kwargs):
for k, v in kwargs.items():
self.__setattr__(k, v)
class Foo:
def __init__(self,a=0):
self.a = a
class Bar(Foo):
def __init__(self,b=21,**kwargs):
self.b = b # accept whatever you want for this child
Foo.__init__(self,**kwargs) # pass remaining kwargs down the line
does exactly what your Foo class does and is much more clear
most problems with using kwargs come from the fact that its not at all self documenting ... I have no idea what arguments can and should be supplied for neither Foo or Bar where as explicitly declared arguments with default values make it very clear what options are available to pass into the functions
There is an existing module I use containing a class that has methods with string arguments that take the form:
existing_object.existing_method("arg1")
or
existing_object.existing_method("arg1:arg2")
The arguments are in a hierarchical structure.
I would like to create a module that objectifies the arguments and makes them methods of the class of the imported module such that use would look like this:
my_object.arg1.my_method()
or
my_object.arg1.arg2.my_method()
my_method() would call existing_method() while passing it the "arg1:arg2" as an argument.
If someone could point me in the right direction to get started I'd appreciate it.
You can do this with a custom __getattr__ that returns special method caller instances:
class MethodCaller(object):
def __init__(self, args, parent):
self.args = args
self.parent = parent
def __getattr__(self, name):
return MethodCaller(self.args + (name,), self.parent)
def my_method(self):
return self.parent.existing_method(':'.join(self.args))
class MyClass(object):
def __getattr__(self, name):
return MethodCaller((name,), self)
def existing_method(self, arg):
print arg
Example:
>>> MyClass().arg1.my_method()
arg1
>>> MyClass().arg1.arg2.my_method()
arg1:arg2
>>> MyClass().foo.bar.my_method()
foo:bar
Thinking about this more clearly I realized that what I really wanted was to be able to use the IPython introspection of modules to navigate the hierarchy. This meant that I simply needed to create objects like this:
class Foo():
def __init__(self, arg):
self.arg = arg
def my_method(self.arg)
arg1 = Foo("arg1")
arg1.arg2 = Foo("arg1:arg2")
I'm pretty new to Python and was looking into using threading for some code via this post:
Python - Using threads or a queue to iterate over a for loop that calls a function
I was wondering why this simple example code errors out to
Error: line 1: TypeError: file <maya console> line 4: __init__() got
an unexpected keyword argument 'A' #
My code:
import threading
class Test(threading.Thread):
def __init__(self, **kwargs):
super(Test, self).__init__( **kwargs)
self.__dict__.update(**kwargs)
A = None
B = 1
test = Test(A = A, B = B)
print test.A
print test.B
My assumption is it has to do with super(Test, self).__init__( **kwargs) call, but I'm not sure how to work around it. My goal is pass in a rather large amount of arguments which is why I'm using **kwargs to begin with.
You're passing the arguments A and B to the Thread constructor, which doesn't need them. Probably you should just call the super constructor with no arguments.
threading.Thread.__init__ expects (at most) group, target, name, args, kwargs and verbose keyword arguments.
Since you have a large number of extra arguments (presumably more than the six that threading.Thread.__init__ expects), then
it may be less work to explicity extract those six and handle the rest with
self.__dict__.update(**kwargs)
import threading
class Test(threading.Thread):
def __init__(self, **kwargs):
super(Test, self).__init__(
**{k: v for k in 'group target name args kwargs verbose'.split()
if k in kwargs})
self.__dict__.update(**kwargs)
A = None
B = 1
test = Test(A=A, B=B)
print test.A
print test.B
Note, if you call __init__ with no arguments:
super(Test, self).__init__()
then a whole bunch of attributes used by threading.Thread will not be set:
class Thread(_Verbose):
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None, verbose=None):
assert group is None, "group argument must be None for now"
_Verbose.__init__(self, verbose)
if kwargs is None:
kwargs = {}
self.__target = target
self.__name = str(name or _newname())
self.__args = args
self.__kwargs = kwargs
self.__daemonic = self._set_daemon()
self.__ident = None
self.__started = Event()
self.__stopped = False
self.__block = Condition(Lock())
self.__initialized = True
# sys.stderr is not stored in the class like
# sys.exc_info since it can be changed between instances
self.__stderr = _sys.stderr
I don't think that is what you want to happen...