English 中文(简体)
生成沙捞越的海关数据类型
原标题:Create custom data type in Python
  • 时间:2022-02-18 04:07:08
  •  标签:
  • python

我希望,标题过于误导,我不敢肯定说我的问题的最佳方式。

I m试图创建(X,Y),协调在Avrea的数据类型。 是否有办法建立“原子数据类型”,以便我有价值物体,但也有一些辅助特性?

到目前为止,我已经做了这一简单分类:

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.tuple = (x, y)

Ideally, I d like to be able to do something like this:

>>> p = Point(4, 5)
>>>
>>> my_x = p.x    # can access the `x` attribute with "dot syntax"
>>>
>>> my_tuple = p  # or can access the tuple value directly
                  # without needing to do `.tuple`, as if the `tuple`
                  # attribute is the "default" attribute for the object

<>0> 我不想简单地展示图象,我知道我可以这样做:_repr__<>/code>。 方法

我试图以某种方式制作一个非常简化的<代码>numpy.ndarray,因为ndarrays 是具有自己属性的一类数据。 我试图看望<代码>numpy。 了解如何做到这一点,但这是在我的头上,即哈。

任何建议都值得赞赏!

最佳回答
问题回答

<>Python 3.12+

PEP 695引进了一种新、更紧凑和明确的方式来创建通用类别和职能。 此外,PEP还采用了一种新方式,以公布使用类型说明的种类,从而设立了AliasType。 因此,你可以这样做。

type Point = tuple[float, float]
# or
type Point[T] = tuple[T, T] # more generic

For example, you can use it like this

type Point = tuple[int, int]
p: Point = (0,0)
print(p) # (0,0)
p = (4,5) # re-assign Point p
print(p[0]) # 4, your "x" 
print(p[1]) # 5, your "y"

根据您的使用情况,这一答案可能无关。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签