Jump to content

Python How can I compact libraries into a simple import

I'm trying to compact these libraries so I can just import the module I make, so I don't have to worry about import more libraries and so I can have simpler code. But every time I run a file with this module imported it gives the error: AttributeError: 'str' object has no attribute 'sqrt'. I've tried changing this code adding different things but it gives the same error.

<

import numpy
import math

class cbrt:
    def __init__(self, num):
        self.num = num

class sqrt:
    def __init__(self, num):
        self.num = num

>

Link to post
Share on other sites

What you've pasted here in and of itself is fine, so it's likely something else causing it. What else is the code doing?

 

21 hours ago, Enderg312 said:

I'm trying to compact these libraries so I can just import the module I make, so I don't have to worry about import more libraries and so I can have simpler code

What do you mean by this? I highly discourage trying to reimplement functions from math or numpy. If you only need these specific fuctions then you can simply import them as follows:

from math import sqrt
from numpy import cbrt
sqrt(4)
cbrt(6)

 

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×