Python: how to use attribute from differenct class
Python: how to use attribute from differenct class
I have some basic class Trader
in trader.py
:
Trader
trader.py
class Trader(object):
def __init__(self, exchange_name, api_key, api_secret):
self.Exchange = Exchange(exchange_name, api_key, api_secret).exchange
and a class Exchange
in exchange.py
Exchange
exchange.py
class Exchange(object):
def __init__(self, name, api_key, api_secret):
self.name = name
exchange_id = name
exchange_class = getattr(ccxt, exchange_id)
self.exchange = exchange_class(
'apiKey': api_key,
'secret': api_secret,
'timeout': 30000,
'enableRateLimit': True,
)
The above works. So I can use self.Exchange
inside the Trader class to use functions against the exchange. But it works only because of this line:
self.Exchange
self.Exchange = Exchange(exchange_name, api_key, api_secret).exchange
I don't like the fact I have to put .exchange after it. I'm just a bit playing and learning python. I'm not very experienced in python programming / I'm also not a coin trader, but I liked the ccxt package.
How can I make this work without needing to putting the .exchange
behind my Exchange(...) or is this normal behaviour? Or do I have to make my exchange class in a different way?
.exchange
Thanks
__new__()
__init__()
2 Answers
2
Since exchange
is an attribute of an instance of the Exchange
class, this is normal behavior.
exchange
Exchange
If you want to access exchange
without needing to put .exchange
after your Exchange
object, you might consider doing away with the Exchange
class entirely—since it seems you only use it to get the instance of the exchange_class
from ccxt—and writing a function instead.
exchange
.exchange
Exchange
Exchange
exchange_class
Here's an example:
def get_exchange(exchange_name, api_key, api_secret):
exchange_class = getattr(ccxt, exchange_id)
return exchange_class(
'apiKey': api_key,
'secret': api_secret,
'timeout': 30000,
'enableRateLimit': True,
)
class Trader:
def __init__(self, exchange_name, api_key, api_secret):
self.exchange = get_exchange(exchange_name, api_key, api_secret)
This way an exchange
will be accessible directly by instances of Trader
.
exchange
Trader
Thanks I'll use this solution. But when I'll add additional functions/attributes to the exchange and make it a class. Then you would just use the .exchange() like I was doing now?
– mealesbia
Aug 26 at 20:43
@mealesbia at that point, it would be better to wrap all functions of the
exchange
object that you'll use in methods of the Exchange
class. Then in Trader.__init__
just set self.exchange = Exchange(...)
. Basically use your Exchange
class instead of accessing the exchange_class
directly from ccxt.– Henry Woody
Aug 26 at 20:47
exchange
Exchange
Trader.__init__
self.exchange = Exchange(...)
Exchange
exchange_class
you need to create an instance if you would like to use an attribute from another class
myInstance = Exchange()
print(myInstance.exchange_id) if you would like to print the name
the scope is local you could also declare a variable global before assigning its value if you would like to use it outside the function or class it is created in
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You might want to look into
__new__()
vs__init__()
.– AChampion
Aug 26 at 20:24