相关疑难解决方法(0)

如何打印Python类?

我有一个包含几个函数的类(其中大部分包含解析smth的代码,获取所有必要的信息并打印出来).我正在尝试打印一个班级,但我得到了smth.喜欢<_ main _.TestClass实例,位于0x0000000003650888>.代码示例:

from lxml import html
import urllib2
url = 'someurl.com'


class TestClass:

    def testFun(self):
        f = urllib2.urlopen(url).read()
        #some code

        print 'Value for ' +url+ ':', SomeVariable

    def testFun2(self):
        f2 = urllib2.urlopen(url).read()
        #some code

        print 'Value2 for ' +url+ ':', SomeVariable2

test = TestClass()
print test
Run Code Online (Sandbox Code Playgroud)

当我在课外打印功能时 - 一切都好.我做错了什么,怎么打印课?

谢谢!

python class

5
推荐指数
1
解决办法
461
查看次数

从列表中打印 __init__ 对象 Python 错误

我正在尝试根据我的班级设置从列表中打印对象。

from math import *
import time

class MyClass:
    def __init__(self, name, age):
        self.name = name
        self.age = age

people = [MyClass("David",17),
MyClass("George",63),
MyClass("Zuck",12),
MyClass("Mark",18)
]

print(people[2])
Run Code Online (Sandbox Code Playgroud)

但它打印出这个: < main .MyClass object at 0x0000000003129128> 我希望它打印“Zuck”

python

5
推荐指数
1
解决办法
1195
查看次数

如何在Python中打印一个对象

我有一本字典,比如:

{'string': u'abc', 'object': <DEMO.Detail object at 0xb5b691ac>}
Run Code Online (Sandbox Code Playgroud)

我怎样才能让它显示 的内容DEMO.Detail object,__dict__就像这样?

{'string': u'abc', 'object': {'obj_string': u'xyz', 'obj_object': {...}}}
Run Code Online (Sandbox Code Playgroud)

python

4
推荐指数
1
解决办法
1万
查看次数

python自定义类运算符重载

假设我有一个班级:

class Cat:
    def __init__(self, name = "default", age = 0):
        self.name = name
        self.age = age
Run Code Online (Sandbox Code Playgroud)

我还有一个猫列表:

l = [Cat('Joe')]
Run Code Online (Sandbox Code Playgroud)

现在我不能打电话给以下人员:

if 'Joe' in l: # the right syntax would be if Cat('Joe') in list
Run Code Online (Sandbox Code Playgroud)

我需要哪个运算符重载才能将identify类Cat对象的by成员变量name?

python oop class operator-overloading

3
推荐指数
1
解决办法
2033
查看次数

如何在Python中不实例化对象的情况下打印类属性?

根据这篇文章,我可以通过访问枚举实例变量str(self.__dict__),但我不知道如何使用类变量来做到这一点。

这是我想避免的:

# I would like to print out class attributes by overriding `__str__` for that class.
class circle(object):
    radius = 3
    def __str__(self):     # I want to avoid instantiation like this.
        return str(circle.radius)

print(circle())    # I want to avoid instantiation. why can't I just print(circle)?
Run Code Online (Sandbox Code Playgroud)

python oop class

3
推荐指数
1
解决办法
2811
查看次数

如何在python中打印/返回一个类?

可能重复:
Python:如何使用print()打印类或类的对象?

我目前有这个代码:

class Track(object):
    def __init__(self,artist,title,album=None):
        self.artist = artist
        self.title = title
        self.album = album

    def __str__(self):
        return self.title + self.artist + self.album
Run Code Online (Sandbox Code Playgroud)

现在,当我把一些东西Track('Kanye West','Roses','Late Registration')放进终端时,我得到<__main__.Track object at 0x10f3e0c50>如何让它返回或在那个地方打印价值?

我是编程新手,特别是'面向对象编程'的新手,所以我的问题是什么是一个类?如何在类中定义函数?

python class object python-2.7

2
推荐指数
1
解决办法
2万
查看次数

在 Python 中创建唯一对象数组

假设我有一个程序可以创建一些带有线和点的方案。所有直线均由两点确定。有这些类:

class Coordinates(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y


class Point(object):
    def __init__(self, coordinates):
        self.coordinates = coordinates

class Line(object):
    def __init__(self, coordinates_1, coordinates_2):
        self.coordinates_1 = coordinates_1
        self.coordinates_2 = coordinates_2
Run Code Online (Sandbox Code Playgroud)

方案采用线列表并创建唯一点列表。

class Circuit(object):
    def __init__(self, element_list):
        self.line_list = element_list
        self.point_collection = set()
        self.point_collection = self.generate_points()

    def generate_points(self):
        for line in self.line_list:
            coordinates_pair = [line.coordinates_1, line.coordinates_2]
            for coordinates in coordinates_pair:
                self.point_collection.add(Point(coordinates))
        return self.point_collection
Run Code Online (Sandbox Code Playgroud)

哪些变体能够创建唯一对象的列表或集合?如何不使用集合和排序,仅使用循环和条件来做到这一点?以及如何更简单地做到这一点?

UPD。我附加的代码无法正常工作。我尝试在 Point 类中添加hash和eq方法:

class Point(object):
def __init__(self, coordinates):
    self.coordinates = coordinates …
Run Code Online (Sandbox Code Playgroud)

python arrays algorithm collections list

2
推荐指数
1
解决办法
4090
查看次数

对象列表的自定义字符串格式

我有一个对象列表.我试图在列表中输出有意义的对象表示,它使用最近版本的Python内置的字符串格式化功能来揭示一些实例变量.我使用的是Python 3.3.0.

我__str__在类中为列表中的对象定义了一个方法.如果我尝试打印单个对象,我将获得该__str__方法返回的表示.但是如果我尝试打印整个列表,我会得到通用表示,就像我没有定义__str__方法一样.这是一个例子:

class CustomClass:
    def __init__(self):
        self.instanceVar = 42

    def __str__(self):
        return "{} (instanceVar = {})".format(self.__class__.__name__, self.instanceVar)

testObject = CustomClass()

print("{}".format(testObject))
# CustomClass (instanceVar = 42)

print("{}".format([testObject]))
# [<__main__.CustomClass object at 0x1006cbed0>]
Run Code Online (Sandbox Code Playgroud)

如何获得第二个打印语句来打印类似的东西[CustomClass (instanceVar = 42)]?是否可以通过将正确的格式字符串传递给print函数来实现这一点,还是比它更复杂?

python string string-formatting python-3.x

1
推荐指数
1
解决办法
1110
查看次数

python nltk中的函数'bigrams'不起作用

来自nltk的函数bigrams返回以下消息,

即使导入nltk并且其他功能正在运行.有任何想法吗?谢谢.

>>> import nltk
>>> nltk.download()
showing info http://www.nltk.org/nltk_data/
True
>>> from nltk import bigrams
>>> bigrams(['more', 'is', 'said', 'than', 'done'])
<generator object bigrams at 0x0000000002E64240>
Run Code Online (Sandbox Code Playgroud)

python nltk

1
推荐指数
1
解决办法
6132
查看次数