» »

Odkritje današnjega dneva: __int__

Odkritje današnjega dneva: __int__

HotBurek ::

Dobro jutro.

Evo, tole sem danes odkril.

Primer spodnje kode:

class FullCool:
    def __int__(self):
        self.width = 0;

def call1():

    fc = FullCool();

    return fc;

if __name__ == "__main__":

    test1 = call1();

    print(test1.width);


In ta koda vrača error:

AttributeError: 'FullCool' object has no attribute 'width'

In nisem našel druge rešitve, kot da v funkciji call1() ponovno nastavil vrednost za width.

Sam vseeno, neki to ne štima, in sem šel vprašat spletnega pomočnika.

In ta pravi:



Zanimiv typo (init vs. int), ker python parser nič ne javi, da je kaj narobe, pa tudi autocomplete v IDE-ju lepo pokaže, da je width property na voljo.

Well done.
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
  • spremenilo: HotBurek ()

socialec ::

se reče temu slučajno operator overloading sicer?

karafeka ::

Ja, ti da mislit kak copy paste nateg so ti generative AI boti, folk pa meče milijarde noter.

Zimonem ::

karafeka je izjavil:

Ja, ti da mislit kak copy paste nateg so ti generative AI boti, folk pa meče milijarde noter.

Odgovor katerega mu je dal ai je bistveno boljši kot so ga na islotechu. Saj najbrž bi tudi na medoverju Dobil boljši odgovor.

Poglejmo kaj pravi deepse3k

The error occurs because the class `FullCool` does not properly initialize the `width` attribute due to two critical mistakes:

1. **Typo in Initializer**: The method name is `__int__` instead of `__init__`.
Python requires the initializer to be named `__init__` to be called automatically when creating an instance.

2. **Missing `self` Parameter in `call1`**:
The method `call1()` is defined without parameters, but it should be called without arguments. The issue here is actually that `call1` is a function, not a method, so it doesn't take `self`. The real problem is in the class definition.

### Corrected Code:
```python
class FullCool:
def __init__(self): # Corrected method name
self.width = 0 # Initialize the attribute

def call1():
fc = FullCool()
return fc

if __name__ == "__main__":
test1 = call1()
print(test1.width) # Output: 0
```

### Key Fixes:
1. Changed `__int__` to `__init__` (correct initializer name).
2. Ensured `__init__` includes `self` as the first parameter.
3. The `width` attribute is now properly initialized when `FullCool` is instantiated.

### Why the Error Occurred:
- Since `__int__` is not the initializer, it was never executed when creating `FullCool()`.
- The instance `fc` had no `width` attribute, causing `AttributeError` when accessed.

The corrected code will now output `0` as expected.

Zato pa so sedaj popularni coding agenti, ker sežejo dlje od avtocomplete

Je pa tema res povsem nedostojna za šlo tech.

Zgodovina sprememb…

  • spremenilo: Zimonem ()

socialec ::

Eni uporabijo konfigurabilne prompt kreatorje za specifikacije in omejitve, se ne zapravlja časa s približevanjem želenemu outputu glede vsebine in formata ampak se prej 'zmodelira' in shrani, mogoče razmisliš kaj v tej smeri? Čez čas postane fine tunanje zoprno, prompt pa daljši - to velja za ekstremne norce, socialci ne kompliciramo toliko, smo navajeni skromnosti.

DamijanD ::

Tega dela pa ne razumem:

2. **Missing `self` Parameter in `call1`**:
The method `call1()` is defined without parameters, but it should be called without arguments. The issue here is actually that `call1` is a function, not a method, so it doesn't take `self`. The real problem is in the class definition.

sealcradle ::

Zato se pa rece "it's a constructor, init?".

garamond ::

DamijanD je izjavil:

Tega dela pa ne razumem:

2. **Missing `self` Parameter in `call1`**:
The method `call1()` is defined without parameters, but it should be called without arguments. The issue here is actually that `call1` is a function, not a method, so it doesn't take `self`. The real problem is in the class definition.
Tega nihče ne razume, še najmanj pa Zimonem in LLM, ki je to spesnil. HotBurek je dobil pravilni odgovor dobesedno v prvi repliki (pa ga niti ni zares potreboval). Tak copy-paste odgovor je pa čisti spam, na ravni LMGTFY odgovora.
A parody of extremism is impossible to differentiate from sincere extremism.

Zimonem ::

Prekleto pa res. Iz tega kosa mesa še nekaj bo. Sedaj nam bo pa dragi garamond razložilil kaj je operator overloading. Kjer ni nikakeršnega operatorja. Potem pa kako se obnaša objekt brez konstruktorja v razredu. Res da natvezil llm stvari. Ampak ko to posvečeno bitje forme požegna operator overloading.... Bruh

Zimonem ::

Aja ob tem da se je llm sposoben revidirati v nekaj sekundah in prizna napako pri funkciji. Ti se ne boš nikoli.


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

python osnova

Oddelek: Programiranje
212477 (1790) detroit
»

android črta

Oddelek: Programiranje
412552 (1823) g333kk
»

[android] crash

Oddelek: Programiranje
61421 (1230) messi
»

Ste pripravljeni na konec sveta?

Oddelek: Loža
141982 (1860) Zero0ne
»

tomcat in datasource

Oddelek: Programiranje
132140 (1897) infiniteLoop

Več podobnih tem