我在python中创建了一个类,当我尝试将其调用到另一个python文件中(导入后)时,它不能将其识别为类,而是一个对象,然后它告诉我该类不可调用< / p>
这是我的课:
class Cell:
def __init__(self,value=9,isVissible=False):
self.value=value
self.isVisible=isVissible
def setValue(self,value):
self.value=value
def setVisible(self):
self.visible=True
这是我试图称呼它的地方:
import Cell,random
class Board:
def __init__(self):
self.board = []
for i in range(12):
a = []
for j in range(12):
x = Cell() <=== right here it's an error
.
.
.(the rest of my program)
最后是错误:
x=Cell()
TypeError: 'module' object is not callable
有人可以帮我解决这个问题,即使我的老师也不明白我的错误
答案 0 :(得分:2)
void Main()
{
List<Person> personList = new List<Person>()
{
new Person{ Name = "Shekhar", Age = 31},
new Person{ Name = "Sandip", Age = 32},
new Person{ Name = "Pramod", Age = 32},
new Person{ Name = "Kunal", Age = 33}
};
var personNameList = personList.Select(p => p.Name).ToList();
var personNameHashset = new HashSet<string>(personNameList);
var nameList = new List<string>() { "Kunal", "Pramod", "Mrinal" };
var finalExpression = personNameHashset.EqualExpression<string>("Name");
var finalFunc = finalExpression.Compile();
var result = nameList.Where(finalFunc);
result.Dump();
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public static class ExpressionTreesExtension
{
public static Expression<Func<T, bool>> EqualExpression<T>(this HashSet<string> nameHashSet, string columnName)
{
var expressionList = new List<Expression>();
ParameterExpression parameterType = Expression.Parameter(typeof(T), "obj");
// Exception Here - How to create MemberExpression for primitive type list List<string>
MemberExpression memberExpressionColumn = Expression.Property(parameterType,columnName);
var containsMethodInfo = typeof(StringListExtensions).GetMethod("Contains", new[] { typeof(string), typeof(HashSet<string>) });
ConstantExpression constant = Expression.Constant(nameHashSet, typeof(HashSet<string>));
var resultExpression = Expression.Call(null, containsMethodInfo, memberExpressionColumn, constant);
return Expression.Lambda<Func<T, bool>>(resultExpression, parameterType);
}
}
public static class StringListExtensions
{
/// <summary>
/// String Extension - Contains (Substring)
/// </summary>
/// <param name="source"></param>
/// <param name="subString"></param>
/// <returns></returns>
public static bool Contains(this string name, HashSet<string> nameHashSet)
{
return nameHashSet.Contains(name);
}
}
已用于您导入的模块和您的类。根据错误,python已将其映射到模块名称。因此,在编写Cell
时,它会尝试将模块名称用作函数,而不是调用类构造函数。
如果类Cell()
在Cell
模块内部,请改用Cell
,或将导入更改为Cell.Cell()
。否则,请重命名模块或类。
答案 1 :(得分:0)
您的导入语句是错误的,您正在导入一个名为Cell的模块,而不是Cell类。您应该使用小写字母作为文件名并按如下所示导入:
from cell import Cell
test = Cell()