create an instance of class in python 2.7

user3054997

I wrote a code:

class NewsStory(object):
    def __init__(self, guid, title, subject, summary, link):
        NewsStory.guid = guid
        NewsStory.title = title
        NewsStory.subject = subject
        NewsStory.summary = summary
        NewsStory.link = link

    def getGuid(self):
        return self.guid

    def getTitle(self):
        return self.title

    def getSubject(self):
        return self.subject

    def getSummary(self):
        return self.summary

    def getLink(self):
        return self.link

When I added an instance as:

test = NewsStory('foo', 'myTitle', 'mySubject', 'some long summary', 'www.example.com')

print test.getGuid() gives me foo, which is correct. However, if I continuously created two instances:

test = NewsStory('foo', 'myTitle', 'mySubject', 'some long summary', 'www.example.com')
test1 = NewsStory('foo1', 'myTitle1', 'mySubject1', 'some long summary1', 'www.example1.com')

both print test.getGuid() and print test1.getGuid() gave me foo1 but no foo. Why does it happen? And is there a method that I can modify my class definition or functions inside the class to avoid the new created instance overwriting the old one?

Thank you.

Matthew

You'll need to make those variables in your __init__ function instance variables instead of class variables.

Instance variables look like this:

self.guid = guid

Class variables look like this:

NewsStory.guid = guid

Class variables are the same for all members of the class, but instance variables are unique to that instance of the class.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

OOP Java: Better to extend a class or create a new instance of the subclass?

来自分类Dev

Cannot create virtualenv instance in python 2.7.5 because of pip installation error

来自分类Dev

Why is Python's lxml.etree.SubElement a class method not an instance method?

来自分类Dev

什么时候dir(class_name)!= dir(instance)在python中

来自分类Dev

如何在python API softlayer create_instance调用中指定private_vlan

来自分类Dev

Static Instance Base/Derived class

来自分类Dev

Access class variable from instance

来自分类Dev

Does this point to the class or a class instance? (6)

来自分类Dev

Windows 7中托管的GExiv2.py(Python)

来自分类Dev

Think Python 2nd Edition 练习 7-1

来自分类Dev

Static function returning a static instance of the class - shouldn't the instance be the same?

来自分类Dev

Create a method attribute in a class

来自分类Dev

KeyError:Python Tkinter中的<class'__main __。Page2'>

来自分类Dev

Is it true that every inner class requires an enclosing instance?

来自分类Dev

WM_CLASS与WM_INSTANCE?

来自分类Dev

Create instance of view controller with correct constructor

来自分类Dev

Castle Windsor re-create instance

来自分类Dev

Call derived class method from base class instance

来自分类Dev

Tomcat7 in debian:wheezy Docker instance fails to start

来自分类Dev

Windows 7 Heroku Python Django LNK2001 psycopg2错误

来自分类Dev

无法在Python 2.7和CentOS 7中导入libxml2和libxslt

来自分类Dev

在Windows 7,Python 3.3上安装RPy2-2.3.8时出错

来自分类Dev

Windows 7 Heroku Python Django LNK2001 psycopg2错误

来自分类Dev

使用Python在VTK-7中进行2D演员挑选

来自分类Dev

Trying to create an instance of a struct with an predefined image. Unsure if struct or instance code is off

来自分类Dev

How can I copy a templated instance with a virtual base class?

来自分类Dev

javascript:相当于新Class()的new instance.constructor()?

来自分类Dev

如何实现`my_class_instance(a,b)= value`操作

来自分类Dev

Redirecting to Amazon EC2 AMI Instance

Related 相关文章

  1. 1

    OOP Java: Better to extend a class or create a new instance of the subclass?

  2. 2

    Cannot create virtualenv instance in python 2.7.5 because of pip installation error

  3. 3

    Why is Python's lxml.etree.SubElement a class method not an instance method?

  4. 4

    什么时候dir(class_name)!= dir(instance)在python中

  5. 5

    如何在python API softlayer create_instance调用中指定private_vlan

  6. 6

    Static Instance Base/Derived class

  7. 7

    Access class variable from instance

  8. 8

    Does this point to the class or a class instance? (6)

  9. 9

    Windows 7中托管的GExiv2.py(Python)

  10. 10

    Think Python 2nd Edition 练习 7-1

  11. 11

    Static function returning a static instance of the class - shouldn't the instance be the same?

  12. 12

    Create a method attribute in a class

  13. 13

    KeyError:Python Tkinter中的<class'__main __。Page2'>

  14. 14

    Is it true that every inner class requires an enclosing instance?

  15. 15

    WM_CLASS与WM_INSTANCE?

  16. 16

    Create instance of view controller with correct constructor

  17. 17

    Castle Windsor re-create instance

  18. 18

    Call derived class method from base class instance

  19. 19

    Tomcat7 in debian:wheezy Docker instance fails to start

  20. 20

    Windows 7 Heroku Python Django LNK2001 psycopg2错误

  21. 21

    无法在Python 2.7和CentOS 7中导入libxml2和libxslt

  22. 22

    在Windows 7,Python 3.3上安装RPy2-2.3.8时出错

  23. 23

    Windows 7 Heroku Python Django LNK2001 psycopg2错误

  24. 24

    使用Python在VTK-7中进行2D演员挑选

  25. 25

    Trying to create an instance of a struct with an predefined image. Unsure if struct or instance code is off

  26. 26

    How can I copy a templated instance with a virtual base class?

  27. 27

    javascript:相当于新Class()的new instance.constructor()?

  28. 28

    如何实现`my_class_instance(a,b)= value`操作

  29. 29

    Redirecting to Amazon EC2 AMI Instance

热门标签

归档