简体   繁体   English

python类声明中的对象参数

[英]Object parameter in python class declaration

Concepts of objects in python classes python类中对象的概念

While reading about old style and new style classes in Python , term object occurs many times.在阅读 Python 中的旧样式和新样式类时,术语对象多次出现。 What is exactly an object?究竟什么是对象? Is it a base class or simply an object or a parameter ?它是基类还是简单的对象或参数?

for eg :例如:

New style for creating a class in python在 python 中创建类的新样式

class Class_name(object):
pass

If object is just another class which is base class for Class_name (inheritance) then what will be termed as object in python ?如果 object 只是另一个类,它是 Class_name (继承)的基类,那么在 python 中什么将被称为 object ?

From [Python 2.Docs]: Built-in Functions - class object ( emphasis is mine):来自[Python 2.Docs]:内置函数 - 类对象重点是我的):

Return a new featureless object.返回一个新的无特征对象。 object is a base for all new style classes . object是所有新样式类的基础 It has the methods that are common to all instances of new style classes.它具有新样式类的所有实例通用的方法。

You could also check [Python]: New-style Classes (and referenced URL s) for more details.您还可以查看[Python]: New-style Classes (and referenced URL s) 了解更多详细信息。

 >>> import sys >>> sys.version '2.7.10 (default, Mar 8 2016, 15:02:46) [MSC v.1600 64 bit (AMD64)]' >>> >>> class OldStyle(): ... pass ... >>> >>> class NewStyle(object): ... pass ... >>> >>> dir(OldStyle) ['__doc__', '__module__'] >>> >>> dir(NewStyle) ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] >>> >>> old_style = OldStyle() >>> new_style = NewStyle() >>> >>> type(old_style) <type 'instance'> >>> >>> type(new_style) <class '__main__.ClassNewStyle'> '''

In the above example, old_style and new_style are instances (or may be referred to as object s), so I guess the answer to your question is: depends on the context.在上面的例子中, old_stylenew_style是实例(或者可能被称为object ),所以我猜你的问题的答案是:取决于上下文。

All objects in python are ultimately derived from "object". python中的所有对象最终都是从“对象”派生的。 You don't need to be explicit about it in python 3, but it's common to explicitly derive from object.您不需要在 python 3 中明确说明它,但通常从 object 中显式派生。

An Object is something in any Object Oriented language including Python, C#, Java and even JavaScript these days.对象是任何面向对象语言中的东西,包括 Python、C#、Java 甚至 JavaScript。 Objects are also prevalent in 3d modeling software but are not per se the same thing.对象在 3D 建模软件中也很普遍,但本质上并不是一回事。

An Object is an INSTANTIATED class. Object 是一个 INSTANTIATED 类。 A class is a blueprint for creating object.类是创建对象的蓝图。 Almost everything in Python is an object. Python 中几乎所有的东西都是对象。

In reference to how you seem to be regarding to exclusivity of objects in python, yes, there is an object class.关于您似乎对python中对象的排他性的看法,是的,有一个对象类。 C# has them too and possibly others where you would use it not dissimilar to creating a string or int. C# 也有它们,可能还有其他你会使用它的地方,与创建字符串或 int 没有什么不同。

Object is a generic term.对象是一个通用术语。 It could be a class, a string, or any type.它可以是一个类、一个字符串或任何类型。 (and probably many other things) (可能还有很多其他的东西)

As an example look at the term OOP, "Object oriented programming".作为一个例子,看看术语 OOP,“面向对象的编程”。 Object has the same meaning here.对象在这里具有相同的含义。

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

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