1

I am using Windows Powershell to import Python file and create instance of one of the class defined with in file as follows:

import random
class RandomWalker:
    def __init__(self):
        self.position = 0

    def walk(self, n):
        self.position = 0
        for i in range(n):
            yield self.position
            self.position += 2*random.randint(0,1) -1

This file is randomWalk.py

So, I run the below command on Python command line:

>>> import randomWalk

But when I try to create an instance of the class it throws an error:

>>> walker = RandomWalker()

Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'RandomWalker' is not defined

What am I missing? I tried to google, I assume we can create an instance of class on Python command line interface.

3

2 Answers 2

4

You are importing only the module, not the class. With the code you provided try:

import randomWalk

walker = randomWalk.RandomWalker()

or for importing the class directly:

from randomWalk import RandomWalker

walker = RandomWalker()
Sign up to request clarification or add additional context in comments.

Comments

3

You should try:

walker = randomWalk.RandomWalker()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.