对象vs实例在python [英] Objects vs instance in python

查看:107
本文介绍了对象vs实例在python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中只有对象和类,其中对象是类的实例。



在Python中,类定义(即类的主体)称为对象。
而且,C ++中的对象在python中被称为实例。



检查这个



我错了吗?



EDIT:解释对象vs实例的差异示例



编辑:在python中,一切都将继承自 object 因此一切都是一个对象(即对象类的对象)。



类也是一个对象/ strong> class)。



Instance是用于调用任何类的对象的名称(aka c ++ object)。



请参阅

解决方案


在Python中,类定义(即类的主体) p>

实际上,这仍然称为Python中的类。这就是为什么你这样定义:

  class Foo(object):
pass

使用关键字,因为结果仍然称为类。 / p>

单词 object 在括号中表示Foo派生自 object 。不要困惑 - 任何现有的类都可以在这里使用;



通常从 object 派生类的原因是历史事故,但可能是值得一个细节。 Python的原始对象实现将用户定义的类和内置类型视为稍微不同的东西。然后语言的设计者决定统一这两个概念。因此,从 object (或从对象的派生)派生的类与 派生自 object ,并被称为新式类。另一方面,旧式类是这样定义的:

  class Foo:
pass

class Bar(Foo):
pass

注意这些继承自 object 或继承自 object 的任何其他内容。



当使用Python 2.x时,你的类几乎总是继承自 object

为了进一步混淆事物,在Python 3.0和更高版本中,没有旧样式类,因此您不必显式地从 object 派生。换句话说,全部上述类将是Python 3.x中的新式类。然而,大多数人仍在使用Python 2.x。



现在,回到现在的问题。类对象,因为一切都是Python中的对象。列表,字典,整数,字符串,元组...所有这些都是对象,Python程序的构建块也是这样:模块,函数和类。您可以使用 class 关键字创建一个类,然后将其传递给一个函数,修改它等。



类是构建对象的模板,称为实例。这个部分你已经知道了。您可以类似于调用函数来实例化对象,传递初始值和其他参数:

  mylist = list(abc) #constructs [a,b,c] 

一个实例,然后调用新实例的 __ init __()方法来初始化它。



要知道,就像类是构建对象的模板一样,因此可以有用于构建类的模板。这些被称为元类。基本元类称为 type (也就是说,普通的新式类是 type )。要创建自己的元类,可以从类型中导出:

  class mymeta(type):
pass

元类是一个相当高级的Python主题,我不会进入你可能使用它们或如何做,但他们应该清楚Python采取一切是一个对象的概念多远。


In C++ there are just objects and classes, where objects are instances of classes.

In Python, a class definition (i.e., the body of a class) is called an object. And, the object in C++ is called instance in python.

Check this

Am I wrong?

EDIT : Actually can someone explain with example difference of object vs instance

EDIT : In python, everything will inherit from object class & hence everything is an object (i.e object of object class).

A Class is also an object (i.e object of object class).

Instance is the name used to call the object of any class.(a.k.a c++ object).

Please refer this

解决方案

In Python, a class definition (i.e., the body of a class) is called an object

Actually, this is still called a class in Python. That's why you define it like this:

class Foo(object):
    pass

The class keyword is used because the result is still called a class.

The word object is in parentheses to show that Foo is derived from the class called object. Don't be confused -- any existing class could be used here; more than one, in fact.

The reason you usually derive classes from object is a historical accident but probably is worth a detail. Python's original object implementation treated user-defined classes and built-in types as slightly different kinds of things. Then the language's designer decided to unify these two concepts. As a result, classes derived from object (or from a descendant of object) behave slightly differently from classes that are not derived from object and are called new-style classes. Old-style classes, on the other hand, were ones defined like this:

class Foo:
    pass

class Bar(Foo):
    pass

Note these do not inherit from object or from anything else that inherits from object. This makes these old-style classes.

When working with Python 2.x, your classes should almost always inherit from object, as the new-style objects are nicer to work with in several minor but important ways.

To further confuse things, in Python 3.0 and later, there are no old-style classes, so you don't have to derive from object explicitly. In other words, all the above classes would be new-style classes in Python 3.x. Most people are still using Python 2.x, however.

Now, back to the matter at hand. Classes are objects because everything is an object in Python. Lists, dictionaries, integers, strings, tuples... all of these are objects, and so are the building blocks of Python programs: modules, functions, and classes. You can create a class using the class keyword and then pass it to a function, modify it, etc.

A class is a template for building objects, which are referred to as instances. This part you already know. You instantiate objects similar to calling a function, passing in the initial values and other parameters:

mylist = list("abc")   # constructs ["a", "b", "c"]

Behind the scenes, this creates an instance, then calls the new instance's __init__() method to initialize it.

One last thing you might want to know is that just as classes are templates for building objects, so it is possible to have templates for building classes. These are called metaclasses. The base metaclass is called type (that is, an ordinary new-style class is an instance of type). To create your own metaclass, you derive it from type like so:

class mymeta(type):
    pass

Metaclasses are a fairly advanced Python topic, so I won't go into what you might use them for or how to do it, but they should make it clear how far Python takes the "everything is an object" concept.

这篇关于对象vs实例在python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆