__call__ or __init__ called here? Don't undestand which and why
up vote
0
down vote
favorite
Edit: this is unfortunately not answered in What is the difference between __init__ and __call__ in Python?
class OAuth2Bearer(requests.auth.AuthBase):
def __init__(self, api_key, access_token):
self._api_key = api_key
self._access_token = access_token
def __call__(self, r):
r.headers['Api-Key'] = self._api_key
r.headers['Authorization'] = "Bearer {}".format(self._access_token)
return r
#############
class AllegroAuthHandler(object):
def apply_auth(self):
return OAuth2Bearer(self._api_key, self.access_token) # what will happen here?
I read about __init__
and __call__
, but I still don't undestand what is going on in this code
I don't understand:
1.) Which method will be called, __init__
or __call__
2.) If __init__
, then __init__
doesn't return anything
3.) If __call__
, then __call__
can't be called with two parameters
I think __init__
should be called, because we have X()
, not x()
from example below as in this answer:
x = X() # __init__ (constructor)
x() # __call__
python class constructor call init
|
show 2 more comments
up vote
0
down vote
favorite
Edit: this is unfortunately not answered in What is the difference between __init__ and __call__ in Python?
class OAuth2Bearer(requests.auth.AuthBase):
def __init__(self, api_key, access_token):
self._api_key = api_key
self._access_token = access_token
def __call__(self, r):
r.headers['Api-Key'] = self._api_key
r.headers['Authorization'] = "Bearer {}".format(self._access_token)
return r
#############
class AllegroAuthHandler(object):
def apply_auth(self):
return OAuth2Bearer(self._api_key, self.access_token) # what will happen here?
I read about __init__
and __call__
, but I still don't undestand what is going on in this code
I don't understand:
1.) Which method will be called, __init__
or __call__
2.) If __init__
, then __init__
doesn't return anything
3.) If __call__
, then __call__
can't be called with two parameters
I think __init__
should be called, because we have X()
, not x()
from example below as in this answer:
x = X() # __init__ (constructor)
x() # __call__
python class constructor call init
Nice explanation here: stackoverflow.com/a/9663601/259889
– Sid
Nov 11 at 5:12
Possible duplicate of What is the difference between __init__ and __call__ in Python?
– Severin Pappadeux
Nov 11 at 5:12
@SeverinPappadeux Unfortunately this is not explaining questions 2 and 3 :-(
– qewghbjhb
Nov 11 at 5:18
@Sid Unfortunately this is not explaining questions 2 and 3 :-(
– qewghbjhb
Nov 11 at 5:18
You're right that__init__
doesn't return anything, but it's called on the new object, which is what gets returned when you call the class. So you could sort of imagine areturn self
and you'd have more or less the right idea.
– Blckknght
Nov 11 at 5:19
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Edit: this is unfortunately not answered in What is the difference between __init__ and __call__ in Python?
class OAuth2Bearer(requests.auth.AuthBase):
def __init__(self, api_key, access_token):
self._api_key = api_key
self._access_token = access_token
def __call__(self, r):
r.headers['Api-Key'] = self._api_key
r.headers['Authorization'] = "Bearer {}".format(self._access_token)
return r
#############
class AllegroAuthHandler(object):
def apply_auth(self):
return OAuth2Bearer(self._api_key, self.access_token) # what will happen here?
I read about __init__
and __call__
, but I still don't undestand what is going on in this code
I don't understand:
1.) Which method will be called, __init__
or __call__
2.) If __init__
, then __init__
doesn't return anything
3.) If __call__
, then __call__
can't be called with two parameters
I think __init__
should be called, because we have X()
, not x()
from example below as in this answer:
x = X() # __init__ (constructor)
x() # __call__
python class constructor call init
Edit: this is unfortunately not answered in What is the difference between __init__ and __call__ in Python?
class OAuth2Bearer(requests.auth.AuthBase):
def __init__(self, api_key, access_token):
self._api_key = api_key
self._access_token = access_token
def __call__(self, r):
r.headers['Api-Key'] = self._api_key
r.headers['Authorization'] = "Bearer {}".format(self._access_token)
return r
#############
class AllegroAuthHandler(object):
def apply_auth(self):
return OAuth2Bearer(self._api_key, self.access_token) # what will happen here?
I read about __init__
and __call__
, but I still don't undestand what is going on in this code
I don't understand:
1.) Which method will be called, __init__
or __call__
2.) If __init__
, then __init__
doesn't return anything
3.) If __call__
, then __call__
can't be called with two parameters
I think __init__
should be called, because we have X()
, not x()
from example below as in this answer:
x = X() # __init__ (constructor)
x() # __call__
python class constructor call init
python class constructor call init
edited Nov 11 at 5:14
asked Nov 11 at 5:10
qewghbjhb
239
239
Nice explanation here: stackoverflow.com/a/9663601/259889
– Sid
Nov 11 at 5:12
Possible duplicate of What is the difference between __init__ and __call__ in Python?
– Severin Pappadeux
Nov 11 at 5:12
@SeverinPappadeux Unfortunately this is not explaining questions 2 and 3 :-(
– qewghbjhb
Nov 11 at 5:18
@Sid Unfortunately this is not explaining questions 2 and 3 :-(
– qewghbjhb
Nov 11 at 5:18
You're right that__init__
doesn't return anything, but it's called on the new object, which is what gets returned when you call the class. So you could sort of imagine areturn self
and you'd have more or less the right idea.
– Blckknght
Nov 11 at 5:19
|
show 2 more comments
Nice explanation here: stackoverflow.com/a/9663601/259889
– Sid
Nov 11 at 5:12
Possible duplicate of What is the difference between __init__ and __call__ in Python?
– Severin Pappadeux
Nov 11 at 5:12
@SeverinPappadeux Unfortunately this is not explaining questions 2 and 3 :-(
– qewghbjhb
Nov 11 at 5:18
@Sid Unfortunately this is not explaining questions 2 and 3 :-(
– qewghbjhb
Nov 11 at 5:18
You're right that__init__
doesn't return anything, but it's called on the new object, which is what gets returned when you call the class. So you could sort of imagine areturn self
and you'd have more or less the right idea.
– Blckknght
Nov 11 at 5:19
Nice explanation here: stackoverflow.com/a/9663601/259889
– Sid
Nov 11 at 5:12
Nice explanation here: stackoverflow.com/a/9663601/259889
– Sid
Nov 11 at 5:12
Possible duplicate of What is the difference between __init__ and __call__ in Python?
– Severin Pappadeux
Nov 11 at 5:12
Possible duplicate of What is the difference between __init__ and __call__ in Python?
– Severin Pappadeux
Nov 11 at 5:12
@SeverinPappadeux Unfortunately this is not explaining questions 2 and 3 :-(
– qewghbjhb
Nov 11 at 5:18
@SeverinPappadeux Unfortunately this is not explaining questions 2 and 3 :-(
– qewghbjhb
Nov 11 at 5:18
@Sid Unfortunately this is not explaining questions 2 and 3 :-(
– qewghbjhb
Nov 11 at 5:18
@Sid Unfortunately this is not explaining questions 2 and 3 :-(
– qewghbjhb
Nov 11 at 5:18
You're right that
__init__
doesn't return anything, but it's called on the new object, which is what gets returned when you call the class. So you could sort of imagine a return self
and you'd have more or less the right idea.– Blckknght
Nov 11 at 5:19
You're right that
__init__
doesn't return anything, but it's called on the new object, which is what gets returned when you call the class. So you could sort of imagine a return self
and you'd have more or less the right idea.– Blckknght
Nov 11 at 5:19
|
show 2 more comments
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
I believe this is what you're looking for.
The behaviour of calling an object in Python is governed by its type's __call__
, so this:
OAuth2Bearer(args)
Is actually this:
type(OAuth2Bearer).__call__(OAuth2Bearer, args)
What is the type of OAuth2Bearer
, also called its "metaclass"? If not type
, the default, then a subclass of type
(this is strictly enforced by Python). From the link above:
If we ignore error checking for a minute, then for regular class instantiation this is roughly equivalent to:
def __call__(obj_type, *args, **kwargs):
obj = obj_type.__new__(*args, **kwargs)
if obj is not None and issubclass(obj, obj_type):
obj.__init__(*args, **kwargs)
return obj
So the result of the call is the result of object.__new__
after passed to object.__init__
. object.__new__
basically just allocates space for a new object and is the only way of doing so AFAIK. To call OAuth2Bearer.__call__
, you would have to call the instance:
OAuth2Bearer(init_args)(call_args)
1
Thank you, it is much clearer now!
– qewghbjhb
Nov 12 at 11:41
Please mark this as the answer if it pleases you :)
– roeen30
Nov 13 at 13:08
Great answer! Very helpful.
– Dr_bitz
Nov 13 at 13:58
add a comment |
up vote
0
down vote
I'd say it's neither here.
The part of code that's causing confusion is
OAuth2Bearer(self._api_key, self.access_token)
You need to know one thing: While OAuth2Bearer
is the name of a class, it's also an object of class type
(a built-in class). So when you write the above line, what's actually called is
type.__call__()
This can be easily verified if you try this code:
print(repr(OAuth2Bearer.__call__))
it will return something like this:
<method-wrapper '__call__' of type object at 0x12345678>
What type.__call__
does and returns is well covered in other questions: It calls OAuth2Bearer.__new__()
to create an object, and then initialize that object with obj.__init__()
, and returns that object.
You can think of the content of OAuth2Bearer(self._api_key, self.access_token)
like this (pseudo-code for illustration purposes)
OAuth2Bearer(self._api_key, self.access_token):
obj = OAuth2Bearer.__new__(OAuth2Bearer, self._api_key, self.access_token)
obj.__init__()
return obj
Do I undestand correctly thatreturn OAuth2Bearer(init_args)
would bereturn new OAuth2Bearer(init_args)
in C# ?
– qewghbjhb
Nov 12 at 11:33
@qewghbjhb I don't know C# but it appears like you're right (I read Java and they're similar so this is a guess).
– iBug
Nov 12 at 15:05
add a comment |
up vote
0
down vote
__init__()
is called when used with Class
__call__()
is called when used with object of Class
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
I believe this is what you're looking for.
The behaviour of calling an object in Python is governed by its type's __call__
, so this:
OAuth2Bearer(args)
Is actually this:
type(OAuth2Bearer).__call__(OAuth2Bearer, args)
What is the type of OAuth2Bearer
, also called its "metaclass"? If not type
, the default, then a subclass of type
(this is strictly enforced by Python). From the link above:
If we ignore error checking for a minute, then for regular class instantiation this is roughly equivalent to:
def __call__(obj_type, *args, **kwargs):
obj = obj_type.__new__(*args, **kwargs)
if obj is not None and issubclass(obj, obj_type):
obj.__init__(*args, **kwargs)
return obj
So the result of the call is the result of object.__new__
after passed to object.__init__
. object.__new__
basically just allocates space for a new object and is the only way of doing so AFAIK. To call OAuth2Bearer.__call__
, you would have to call the instance:
OAuth2Bearer(init_args)(call_args)
1
Thank you, it is much clearer now!
– qewghbjhb
Nov 12 at 11:41
Please mark this as the answer if it pleases you :)
– roeen30
Nov 13 at 13:08
Great answer! Very helpful.
– Dr_bitz
Nov 13 at 13:58
add a comment |
up vote
2
down vote
accepted
I believe this is what you're looking for.
The behaviour of calling an object in Python is governed by its type's __call__
, so this:
OAuth2Bearer(args)
Is actually this:
type(OAuth2Bearer).__call__(OAuth2Bearer, args)
What is the type of OAuth2Bearer
, also called its "metaclass"? If not type
, the default, then a subclass of type
(this is strictly enforced by Python). From the link above:
If we ignore error checking for a minute, then for regular class instantiation this is roughly equivalent to:
def __call__(obj_type, *args, **kwargs):
obj = obj_type.__new__(*args, **kwargs)
if obj is not None and issubclass(obj, obj_type):
obj.__init__(*args, **kwargs)
return obj
So the result of the call is the result of object.__new__
after passed to object.__init__
. object.__new__
basically just allocates space for a new object and is the only way of doing so AFAIK. To call OAuth2Bearer.__call__
, you would have to call the instance:
OAuth2Bearer(init_args)(call_args)
1
Thank you, it is much clearer now!
– qewghbjhb
Nov 12 at 11:41
Please mark this as the answer if it pleases you :)
– roeen30
Nov 13 at 13:08
Great answer! Very helpful.
– Dr_bitz
Nov 13 at 13:58
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
I believe this is what you're looking for.
The behaviour of calling an object in Python is governed by its type's __call__
, so this:
OAuth2Bearer(args)
Is actually this:
type(OAuth2Bearer).__call__(OAuth2Bearer, args)
What is the type of OAuth2Bearer
, also called its "metaclass"? If not type
, the default, then a subclass of type
(this is strictly enforced by Python). From the link above:
If we ignore error checking for a minute, then for regular class instantiation this is roughly equivalent to:
def __call__(obj_type, *args, **kwargs):
obj = obj_type.__new__(*args, **kwargs)
if obj is not None and issubclass(obj, obj_type):
obj.__init__(*args, **kwargs)
return obj
So the result of the call is the result of object.__new__
after passed to object.__init__
. object.__new__
basically just allocates space for a new object and is the only way of doing so AFAIK. To call OAuth2Bearer.__call__
, you would have to call the instance:
OAuth2Bearer(init_args)(call_args)
I believe this is what you're looking for.
The behaviour of calling an object in Python is governed by its type's __call__
, so this:
OAuth2Bearer(args)
Is actually this:
type(OAuth2Bearer).__call__(OAuth2Bearer, args)
What is the type of OAuth2Bearer
, also called its "metaclass"? If not type
, the default, then a subclass of type
(this is strictly enforced by Python). From the link above:
If we ignore error checking for a minute, then for regular class instantiation this is roughly equivalent to:
def __call__(obj_type, *args, **kwargs):
obj = obj_type.__new__(*args, **kwargs)
if obj is not None and issubclass(obj, obj_type):
obj.__init__(*args, **kwargs)
return obj
So the result of the call is the result of object.__new__
after passed to object.__init__
. object.__new__
basically just allocates space for a new object and is the only way of doing so AFAIK. To call OAuth2Bearer.__call__
, you would have to call the instance:
OAuth2Bearer(init_args)(call_args)
edited Nov 11 at 9:31
answered Nov 11 at 9:25
roeen30
43629
43629
1
Thank you, it is much clearer now!
– qewghbjhb
Nov 12 at 11:41
Please mark this as the answer if it pleases you :)
– roeen30
Nov 13 at 13:08
Great answer! Very helpful.
– Dr_bitz
Nov 13 at 13:58
add a comment |
1
Thank you, it is much clearer now!
– qewghbjhb
Nov 12 at 11:41
Please mark this as the answer if it pleases you :)
– roeen30
Nov 13 at 13:08
Great answer! Very helpful.
– Dr_bitz
Nov 13 at 13:58
1
1
Thank you, it is much clearer now!
– qewghbjhb
Nov 12 at 11:41
Thank you, it is much clearer now!
– qewghbjhb
Nov 12 at 11:41
Please mark this as the answer if it pleases you :)
– roeen30
Nov 13 at 13:08
Please mark this as the answer if it pleases you :)
– roeen30
Nov 13 at 13:08
Great answer! Very helpful.
– Dr_bitz
Nov 13 at 13:58
Great answer! Very helpful.
– Dr_bitz
Nov 13 at 13:58
add a comment |
up vote
0
down vote
I'd say it's neither here.
The part of code that's causing confusion is
OAuth2Bearer(self._api_key, self.access_token)
You need to know one thing: While OAuth2Bearer
is the name of a class, it's also an object of class type
(a built-in class). So when you write the above line, what's actually called is
type.__call__()
This can be easily verified if you try this code:
print(repr(OAuth2Bearer.__call__))
it will return something like this:
<method-wrapper '__call__' of type object at 0x12345678>
What type.__call__
does and returns is well covered in other questions: It calls OAuth2Bearer.__new__()
to create an object, and then initialize that object with obj.__init__()
, and returns that object.
You can think of the content of OAuth2Bearer(self._api_key, self.access_token)
like this (pseudo-code for illustration purposes)
OAuth2Bearer(self._api_key, self.access_token):
obj = OAuth2Bearer.__new__(OAuth2Bearer, self._api_key, self.access_token)
obj.__init__()
return obj
Do I undestand correctly thatreturn OAuth2Bearer(init_args)
would bereturn new OAuth2Bearer(init_args)
in C# ?
– qewghbjhb
Nov 12 at 11:33
@qewghbjhb I don't know C# but it appears like you're right (I read Java and they're similar so this is a guess).
– iBug
Nov 12 at 15:05
add a comment |
up vote
0
down vote
I'd say it's neither here.
The part of code that's causing confusion is
OAuth2Bearer(self._api_key, self.access_token)
You need to know one thing: While OAuth2Bearer
is the name of a class, it's also an object of class type
(a built-in class). So when you write the above line, what's actually called is
type.__call__()
This can be easily verified if you try this code:
print(repr(OAuth2Bearer.__call__))
it will return something like this:
<method-wrapper '__call__' of type object at 0x12345678>
What type.__call__
does and returns is well covered in other questions: It calls OAuth2Bearer.__new__()
to create an object, and then initialize that object with obj.__init__()
, and returns that object.
You can think of the content of OAuth2Bearer(self._api_key, self.access_token)
like this (pseudo-code for illustration purposes)
OAuth2Bearer(self._api_key, self.access_token):
obj = OAuth2Bearer.__new__(OAuth2Bearer, self._api_key, self.access_token)
obj.__init__()
return obj
Do I undestand correctly thatreturn OAuth2Bearer(init_args)
would bereturn new OAuth2Bearer(init_args)
in C# ?
– qewghbjhb
Nov 12 at 11:33
@qewghbjhb I don't know C# but it appears like you're right (I read Java and they're similar so this is a guess).
– iBug
Nov 12 at 15:05
add a comment |
up vote
0
down vote
up vote
0
down vote
I'd say it's neither here.
The part of code that's causing confusion is
OAuth2Bearer(self._api_key, self.access_token)
You need to know one thing: While OAuth2Bearer
is the name of a class, it's also an object of class type
(a built-in class). So when you write the above line, what's actually called is
type.__call__()
This can be easily verified if you try this code:
print(repr(OAuth2Bearer.__call__))
it will return something like this:
<method-wrapper '__call__' of type object at 0x12345678>
What type.__call__
does and returns is well covered in other questions: It calls OAuth2Bearer.__new__()
to create an object, and then initialize that object with obj.__init__()
, and returns that object.
You can think of the content of OAuth2Bearer(self._api_key, self.access_token)
like this (pseudo-code for illustration purposes)
OAuth2Bearer(self._api_key, self.access_token):
obj = OAuth2Bearer.__new__(OAuth2Bearer, self._api_key, self.access_token)
obj.__init__()
return obj
I'd say it's neither here.
The part of code that's causing confusion is
OAuth2Bearer(self._api_key, self.access_token)
You need to know one thing: While OAuth2Bearer
is the name of a class, it's also an object of class type
(a built-in class). So when you write the above line, what's actually called is
type.__call__()
This can be easily verified if you try this code:
print(repr(OAuth2Bearer.__call__))
it will return something like this:
<method-wrapper '__call__' of type object at 0x12345678>
What type.__call__
does and returns is well covered in other questions: It calls OAuth2Bearer.__new__()
to create an object, and then initialize that object with obj.__init__()
, and returns that object.
You can think of the content of OAuth2Bearer(self._api_key, self.access_token)
like this (pseudo-code for illustration purposes)
OAuth2Bearer(self._api_key, self.access_token):
obj = OAuth2Bearer.__new__(OAuth2Bearer, self._api_key, self.access_token)
obj.__init__()
return obj
answered Nov 11 at 5:20
iBug
16.4k53359
16.4k53359
Do I undestand correctly thatreturn OAuth2Bearer(init_args)
would bereturn new OAuth2Bearer(init_args)
in C# ?
– qewghbjhb
Nov 12 at 11:33
@qewghbjhb I don't know C# but it appears like you're right (I read Java and they're similar so this is a guess).
– iBug
Nov 12 at 15:05
add a comment |
Do I undestand correctly thatreturn OAuth2Bearer(init_args)
would bereturn new OAuth2Bearer(init_args)
in C# ?
– qewghbjhb
Nov 12 at 11:33
@qewghbjhb I don't know C# but it appears like you're right (I read Java and they're similar so this is a guess).
– iBug
Nov 12 at 15:05
Do I undestand correctly that
return OAuth2Bearer(init_args)
would be return new OAuth2Bearer(init_args)
in C# ?– qewghbjhb
Nov 12 at 11:33
Do I undestand correctly that
return OAuth2Bearer(init_args)
would be return new OAuth2Bearer(init_args)
in C# ?– qewghbjhb
Nov 12 at 11:33
@qewghbjhb I don't know C# but it appears like you're right (I read Java and they're similar so this is a guess).
– iBug
Nov 12 at 15:05
@qewghbjhb I don't know C# but it appears like you're right (I read Java and they're similar so this is a guess).
– iBug
Nov 12 at 15:05
add a comment |
up vote
0
down vote
__init__()
is called when used with Class
__call__()
is called when used with object of Class
add a comment |
up vote
0
down vote
__init__()
is called when used with Class
__call__()
is called when used with object of Class
add a comment |
up vote
0
down vote
up vote
0
down vote
__init__()
is called when used with Class
__call__()
is called when used with object of Class
__init__()
is called when used with Class
__call__()
is called when used with object of Class
edited Nov 11 at 5:50
eyllanesc
69k93052
69k93052
answered Nov 11 at 5:49
rajamohan reddy
212
212
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246013%2fcall-or-init-called-here-dont-undestand-which-and-why%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Nice explanation here: stackoverflow.com/a/9663601/259889
– Sid
Nov 11 at 5:12
Possible duplicate of What is the difference between __init__ and __call__ in Python?
– Severin Pappadeux
Nov 11 at 5:12
@SeverinPappadeux Unfortunately this is not explaining questions 2 and 3 :-(
– qewghbjhb
Nov 11 at 5:18
@Sid Unfortunately this is not explaining questions 2 and 3 :-(
– qewghbjhb
Nov 11 at 5:18
You're right that
__init__
doesn't return anything, but it's called on the new object, which is what gets returned when you call the class. So you could sort of imagine areturn self
and you'd have more or less the right idea.– Blckknght
Nov 11 at 5:19