1

I created a class within another file and I want to use it as an object in another file (I don't know if I'm using the right words, sorry. I haven't programmed in a few years).

The following code is the tube class that I've created:

class Tube:
    def __init__(self, inner_diameter, length):
        self.inner_diameter = inner_diameter
        self.length = length

    def getLength(self):
        return self.len

    def getInnerDiameter(self):
        return self.inner_dia

Then I called the following in another file:

import tube
tube1 = tube(0.18, 1)

I am getting the following error:

TypeError: 'module' object is not callable

Am I doing something completely wrong? When I looked stuff up, I was getting something that I had to do a call function within my class to get it, but that didn't do anything. Sorry if this is a really obvious question, I am new to python and haven't coded in a quite a while and I only formally learned java.

1
  • 2
    Maybe tube.Tube(args)? Commented Nov 28, 2019 at 20:12

2 Answers 2

1

A couple of quick edits to your code:

class Tube:
    def __init__(self, inner_diameter, length):
        self.inner_diameter = inner_diameter
        self.length = length

    def getLength(self):
        return self.length       # rather than len

    def getInnerDiameter(self):
        return self.inner_diameter     # rather than inner_dia

then your statement to create an instance of tube:

import tube
tube1 = tube.Tube(0.18, 1)  # rather than Tube(0.18, 1)

This specifically calls the init method of the Tube class within the tube module (tube1 is an instance of a Tube from the tube module).

It's comparable to working with any other module. For example:

import math
print(math.pi)

... you need to specify the module before the method.

Sign up to request clarification or add additional context in comments.

Comments

1

As long as your class and main.py or the file you're calling Tube.py from, are in the same folder, you should have no problem doing this:

from tube import Tube

I am new to python and haven't coded in a quite a while and I only formally learned java

Now is a good time to learn how to do things in Python.

You can write properties instead of getters and setters like this:

from typing import final

@ final
class Tube:
    def __init__(self, inner_diameter, length):
        # Note the underscore, in Python this indicates the variable show be treated as 
        # private.

        # The : [int] and [float] are type hints.
        self._inner_diameter  : float = inner_diameter
        self._length : int = length

    @property
    def Length(self):
        return self.length

    @Length.setter
    def Length(self, value):
        self._length = value

    @property
    def getInnerDiameter(self):
        return self.inner_diameter

    def __str__(self):
        return f"Inner Diameter: {self._inner_diameter} Length: {self._length}"

Use it like this:

# Note: the keyword arguments.
obj = Tube(inner_diameter=0,length=1)
obj.Length = 5
print(obj.Length) # Calling the Length getter
# Calling the internal __str__
print(obj)

You can also declare a class final.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.