简体   繁体   English

无法识别来自导入脚本的Python方法

[英]Python method from imported script not recognised

I am using anaconda notebook as my main python tool. 我正在使用anaconda笔记本作为主要的python工具。 I have a script called tree.py and a notebook test.ipynb 我有一个名为tree.py的脚本和一个笔记本test.ipynb

this is what I have in tree.py : 这就是我在tree.py中所tree.py

class bst(object):
   def __init__(self,val):
      self.val = val
      self.left = None
      self.right = None
   def setleft(self,l):
      l = bst(l)
      self.left = l

I imported it in the ipynb and tried to do this: 我将其导入ipynb并尝试执行以下操作:

s = tree.bst(2)
s.setleft(3)

and it gave me an AttributeError saying "bst object has no attribute setleft". 它给了我一个AttributeError,说“ bst对象没有属性setleft”。 What is wrong with my code? 我的代码有什么问题?

In the second file you've created, put in this line of code at the beginning (please make sure that the two files are in the same directory) 在您创建的第二个文件中,在代码行的开头(请确保两个文件位于同一目录中)

import tree

and after write: 然后写:

s = tree.bst(2)
s.setleft(3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM